Let's briefly describe a unique feature of Ruby. block, a piece of code that can be associated with a method call, almost as if it were a parameter. This is an incredibly powerful feature.
You can use the block implementation callback (but it is simpler than the Java anonymous internal (anonymous inner) class), passing a set of code (but it is far more flexible than the function pointers of C), and implementing iterators.
Block is just a set of code between curly braces or do...end.
{puts "Hello"} #this is a block
does ###
club.enroll (person) #and The so are this
person.socialize # end # ##
Why are there two kinds of delimiters? Part of the reason is that some people feel that sometimes it's more natural to use a delimiter than another. Another part of the reason is that they have different priorities: the curly braces are more tightly bound than the do/end. We try to follow a common practice that is becoming a ruby standard, with a single block with curly braces, and multiple lines of block with Do/end.
Once a block is created, it can be associated with a call to a method. The association can be implemented by placing the block at the end of the source line containing the method call. For example, in the following code, a block containing puts "Hi" is associated with a call to a greet method.
If the method has arguments, they appear before the block.
Verbose_greet ("Dave", "Loyal customer") {puts "Hi"}
Then, using Ruby's yield statement, the method can invoke (invoke) associated block one or more times. You can think of yield as a method call, which invokes the block associated with the method that contains the yield statement.
The following example shows how to use the yield statement. Defines a method that will call yield two times. This method is then called to place the block on the same line after the method call (and after all parameters of the method).
def Call_block
puts "Start of method"
yield
yield
puts "end of method"
end
call_block{puts " The Block}
Output results:
Strat in the "block" of method
You can provide a call to yield, and arguments are passed to the block. In the block, the parameter names are given between the vertical bars (|) to accept these arguments from the yield.
def call_block
Yield ("Hello",) end
call_block {|str,num| ...}
A large number of blocks are used in the Ruby Library to implement iterators; An iterator is a method of continuously returning elements from a collection (collection) in an array.
Animals =%w (ant bee Cat dog Elk) #创建一个数组
animals.each{|animal| puts animal} #迭代它的内容
Output results:
Let's look at how to implement each iterator in the array class that is applied in the previous example. Each iterator loops through the elements in the array and invokes yield for each element. In pseudo code, it may be written as:
#在Array类中 ... def each for each
element #<--Invalid RUBY statement
yield (element)
End
Many of the looping constructs built in languages such as C and Java are only method calls in Ruby, and these methods call the associated block 0 or more times.
[' Cat ', ' dog ', ' horse '].each{|name| print name, ' "}
5.times {print" * "}
3.upto (6) {|i| Print i}
(' A ' ... ') E '). each{|char| Print Char}
Output results:
Cat Dog Horse *****3456ABCDE
The code above requires the object to call Block 55 times, and then request object 3 to call a block and pass in a contiguous value until the value reaches 6. Finally, the block is called using the each method for the character interval (range) of a through E.