module which most collections mix in as Section 37 describes Figure 38 shows

Module which most collections mix in as section 37

This preview shows page 123 - 126 out of 517 pages.

module, which most collections mix in, as Section 3.7 describes. Figure 3.8 shows how this unusual construct works. When a method containing yield is called, it starts executing until yield is reached; at that point, control is transferred to the block that was passed to the method. If yield had any arguments, those arguments become the arguments to the block. A common use of yield is implementing iterators like each and every_nth . Unlike Java, in which you have to create an iterator by passing it a collection of some type and then repeatedly call while (iter.hasNext()) and iter.getNext() , Ruby iterators allow turning the control structure “inside-out” and letting data structures manage their own iteration. Don’t confuse this use of the term yield with the unrelated usage from operating systems, in which one thread or process is said to yield to another by giving up the CPU. yield also enables reuse in situations where you need to “sandwich” some custom functionality inside of some common functionality. For example, consider an app that creates HTML pages and uses a standard HTML template for most pages that looks like this, where the only difference between
Image of page 123
different pages is captured by line 8: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Report</title> 5 </head> 6 <body> 7 <div id="main"> 8 ...user-generated content here... 9 </div> 10 </body> 11 </html> In most languages, we could encapsulate the code that generates the boilerplate in lines 1–7 and 9–11 in methods called make_header and make_footer , and then require each method that wants to generate a page to do this: 1 def one_page 2 page = ’’ 3 page << make_header() 4 page << "Hello" 5 page << make_footer() 6 end 7 def another_page 8 page = ’’ 9 page << make_header() 10 page << "World" 11 page << make_footer() 12 end Since this code looks repetitive, we might instead wrap up both calls in a single method: 1 def make_page(contents) 2 page = ’’ 3 page << make_header() 4 page << contents 5 page << make_footer() 6 end 7 # 8 def one_page 9 make_page("Hello") 10 end 11 def another_page 12 make_page("World") 13 end
Image of page 124
But in Chapter 2 we learned that useful design patterns arise from the desire to separate the things that change from those that stay the same. yield provides a better way to encapsulate the common part— the boilerplate “around” the user content—in its own method: 1 def make_page 2 page = ’’ 3 page << make_header() 4 page << yield 5 page << make_footer() 6 end 7 def one_page 8 make_page do 9 "Hello" 10 end 11 end 12 def another_page 13 make_page do 14 "World" 15 end 16 end In this example, when one_page calls make_page , the yield at line 4 returns control to the block at line 9. The block executes, and its return value (in this case, ”Hello” ) is returned to line 4 as the result of the yield , and gets appended to page (using the << operator), after which make_page continues.
Image of page 125
Image of page 126

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors