In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today I will show you how to understand the flexible coding method Ruby blocks. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
The emergence of Ruby language has changed the traditional way of writing code for programmers, so that programmers will have a very happy mood when writing code. Here we introduce a flexible coding method Ruby blocks.
Let's take a look at this code first:
Class SongList
Def [] (key)
Return @ songs [key] if
Key.kind_of? (Integer)
Return @ songs.find {
| | aSong | aSong.name = = key} |
End
End
In the fourth line, there is a method called find that traverses the songs according to the specified conditions, and finally returns an individual that meets the criteria.
Let's take a look at how this method is implemented.
Class Array def find for i in 0size value = self [I] return value if yield (value) end return nil end end
It is found that a method has been added to the class Array and a traversal operation has been embedded in method. If that's the case, then ruby is no different from other languages, and we notice that there is something like yield in line 5. In fact, he plays the role of an agent, realizing the separation of the actual operation part and traversal.
Take a look at the following example to see what yield does.
1def threeTimes yield end threeTimes {puts "Hello"}
A blocks called threeTimes is defined here. The external operation is repeated three times in Ruby blocks. When the code of row 6 is executed, you will get the following result:
Hello
Hello
Hello
You can see that blocks provides us with such a flexible means that it needs to be implemented through proxies or interfaces or function pointers in his language.
In fact, later versions of. Net 3.x provide a similar feature, something called LINQ (Language Integrated Query).
Collections can be filtered in a way similar to SQL
LINQ Query:
String [] names = {"Geoff"
"Jessica", "Mike", "Megan"
"Priscilla", "Jack", "Alma"}
IEnumerable expr =
From s in names
Where s.Length = = 5
Orderby s
Select s.ToUpper ()
Foreach (string item in expr)
Console.WriteLine (item)
Is the above usage very simple and convenient? If you implement it in ruby, it will look like this:
Names = ["Geoff", "Jessica"
"Mike", "Megan", "Priscilla"
"Jack", "Alma"]
Expr = names.select {
| | n | n.length = = 5 |
}. Sort.collect {| n | n.upcase}
Expr.each {| n | puts n}
Because Ruby blocks is so convenient, when reading ruby programs, you can see that it is widely used.
The above is the whole content of how the flexible coding method Ruby blocks understands. For more content related to how the flexible coding method Ruby blocks understands, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.