Ruby keywords yield
The ruby keyword yield is a commonly used keyword in actual programming. Programmers who have just learned the ruby language must first master the usage of this type of basic keywords.
Some basic keywords in ruby are essential for beginners. Here we will take a look at the distinctive Ruby keyword yield.
- Ruby string processing function summary list
- Ruby decoration mode application tips
- Ruby watir environment setup error Solution
- Recommendation of several high-performance Ruby on Rails development plug-ins
- Understanding Ruby truth through Ruby Source
Input
- Def call_block
- Puts "Start of method"
- Yield
- Yield
- Puts "End of method"
- End
- Call_block {puts "in the block "}
Output:
Start of Method
In the block
In the block
End of Method
The usage of yield is different from that on the Internet. Some are placeholders, some are "Give way", and some are macros.
Http://www.javaeye.com/topic/31752
Http://axgle.javaeye.com/blog/31018
In my opinion, the. NET developer is more willing to regard it as a method delegate, which can be written in. net.
Input
- Delegate outhandler ();
- Void call_block (outhandler yield)
- {
- Console. writeline ("Start of method ");
- Yield ();
- Yield ();
- Console. writeline ("End of method ");
- }
- Void test () {console. writeline
("In the block ");}
- // Call
- Call_block (test );
Haha, the above Code seems to be much more redundant than Ruby, but it is also much more rigorous. I don't know if my analysis is correct, but it cannot be completely replaced, if the function defines a variable:
- def call_block
- puts "Start of method"
- @count=1
- yield
- yield
- puts "End of method"
- end
- call_block { puts @count=@count+1 }
Output:
Start of Method
2
3
End of Method
That is to say, this proxy needs to obtain access to the context scope. NET may not be implemented. This is a bit like a macro, or even code weaving. However, it seems that macros cannot be passed through methods. Because of this, Ruby's yield is quite distinctive. Let's take a look at some of his examples:
Input
- [ 'cat', 'dog', 'horse' ].each
{|name| print name, " " }
- 5.times { print "*" }
- 3.upto(6) {|i| print i }
- ('a'..'e').each {|char| print char }
Output:
Cat dog horse ***** 3456 ABCDE
Hey, you can handle all these implementations of. Net delegation, and give a function pointer...
From: http://developer.51cto.com/art/200912/170864.htm