A perfect understanding of the yield concept in ruby

Source: Internet
Author: User
Tags define find
In the university, a book is often placed in the course seat the night before, indicating that the position has been occupied. The next day, I came to this seat and opened the book for formal classes. in this phenomenon, Books Act as placeholders.
In Ruby, yield is a placeholder. First, yield is used in a part of the previous code to occupy the position, and then it is truly implemented in a code block, so as to complete the process of adding a pair of numbers to the seat.
  1. # Define find
  2. Def find (DIR)
  3. Dir. entries (DIR ). each {| f | yield f} # obtain the list of file names in the Dir directory. For each file name, yield is used for processing (as for how to handle it, I still don't know how to take the place first ^_^)
  4. End
  5. # Use Find
  6. Find (".") Do | f | # Start Block
  7. Puts F # Use the output file name statement to implement yield processing (you can also use any other statement)
  8. End # block end
# Define finddef find (DIR) dir. entries (DIR ). each {| f | yield f} # obtain the list of file names in the Dir directory. For each file name, yield is used for processing (as for how to handle it, I still don't know how to take the place first ^_^) end # Use findfind (". ") Do | f | # block start puts f # Use the output file name statement to implement yield processing (or use any other statement) end # block end


It can be seen that yield belongs to the definition layer and belongs to the declaration layer, that is, to say in my mind: "This is a good position. I will use books to take up the space first! "; And block belongs to the use layer, the Implementation Layer, that is, you finally sit in the position you previously occupied, so as to truly implement the process of seat check.
Finally, please do not ask me "what if the book is stolen? "Thank you for your cooperation.

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.