Ruby blocks provides flexible encoding methods

Source: Internet
Author: User

The emergence of the Ruby language has changed the traditional coding method of programmers, so that programmers can have a very pleasant mood when writing code. Here we will introduce you to Ruby blocks, a flexible encoding method.

  • Ruby Rmagick Installation Guide
  • Ruby special syntax concepts
  • Example of Ruby code for getting the current class name
  • Ruby module OpenURI to get http/FTP address content
  • Analysis of Ruby encryption implementation code examples

Let's take a look at this Code:

  1. Class SongList
  2. Def [] (key)
  3. Return @ songs [key] if
    Key. kind_of? (Integer)
  4. Return @ songs. find {
    | ASong. name = key}
  5. End
  6. End

In the fourth row, a method such as find can traverse the songs according to the specified conditions, and finally return an individual that meets the conditions.

Next let's take a look at how this method is implemented.

 
 
  1. class Array   
  2. def find   
  3. for i in 0size   
  4. value = self[i]   
  5. return value if yield(value)   
  6. end   
  7. return nil   
  8. end   
  9. end  

It is found that a method is added to the Array class and a traversal operation is embedded in the method. If this is the case, there will be no difference between ruby and other languages. We have noticed that yield exists in line 1, such a stuff. In fact, he acts as a proxy, realizing the separation of actual operation parts and traversal.

Let's take a look at the example below to understand the yield function.

 
 
  1. 1def threeTimes  
  2. yield  
  3. yield  
  4. yield  
  5. end  
  6. threeTimes { puts "Hello" } 

Here we define the blocks named threeTimes. The Ruby blocks will repeat three external operations. After row 6 code is executed, we will get the following results:
Hello
Hello
Hello

We can see that blocks provides us with such flexible means. In fact, blocks needs to be implemented through proxies, interfaces, or function pointers.
In fact, Versions later than. net 3.x also provide similar functions, a plug-in called LINQLanguage Integrated Query.
You can use SQL-like methods to filter sets.

 
 
  1. LINQ Query:  
  2. string[] names = { "Geoff", 
    "Jessica", "Mike", "Megan",  
  3. "Priscilla", "Jack", "Alma" };  
  4. IEnumerable  expr = 
    from s in names  
  5. where s.Length == 5  
  6. orderby s  
  7. select s.ToUpper();  
  8. foreach (string item in expr)  
  9. Console.WriteLine(item); 

Is the above usage simple and convenient? If ruby is used for implementation, it will be like this:

 
 
  1. names = [ "Geoff", "Jessica", 
    "Mike", "Megan", "Priscilla",  
  2. "Jack", "Alma" ]   
  3. expr = names.select {  
  4. |n| n.length == 5  
  5. }.sort.collect { |n| n.upcase }  
  6. expr.each {|n| puts n } 

Ruby blocks is so convenient that it is widely used when reading ruby programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.