Ruby keywords yield

Source: Internet
Author: User

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

  1. Def call_block
  2. Puts "Start of method"
  3. Yield
  4. Yield
  5. Puts "End of method"
  6. End
  7. 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

 
 
  1. Delegate outhandler ();
  2. Void call_block (outhandler yield)
  3. {
  4. Console. writeline ("Start of method ");
  5. Yield ();
  6. Yield ();
  7. Console. writeline ("End of method ");
  8. }
  9. Void test () {console. writeline
    ("In the block ");}
  10. // Call
  11. 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:

 
 
  1. def call_block   
  2. puts "Start of method"   
  3. @count=1   
  4. yield   
  5. yield   
  6. puts "End of method"   
  7. end   
  8. 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

 
 
  1. [ 'cat', 'dog', 'horse' ].each
     {|name| print name, " " }   
  2. 5.times { print "*" }   
  3. 3.upto(6) {|i| print i }   
  4. ('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

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.