printmovies and therefore not visible to the each block

Printmovies and therefore not visible to the each

This preview shows page 117 - 120 out of 517 pages.

print_movies , and therefore not visible to the each block: 1 def print_movies(movie_list) 2 movie_list.each do |m| 3 puts "#{m.title} #{separator} #{m.rating}" # === FAILS!! === 4 end 5 end 6 separator = ’=>’ 7 print_movies(movies) # FAILS! In programming-language parlance, a Ruby block is a closure : whenever the block executes, it can “see” the entire lexical scope available at the place where the block appears in the program text. In other words, it’s as if the presence of the block creates an instant snapshot of the scope, which can be reconstituted later whenever the block executes. This fact is exploited by many Rails features that
Image of page 117
improve DRYness, including view rendering (which we’ll see in Section 4.4 ) and model validations and controller filters (Section 5.1 ), because they allow separating the definition of what is to occur from when in time and where in the structure of the application it occurs. The fact that blocks are closures should help explain the following apparent anomaly. If the first reference to a local variable is inside a block, that variable is “captured” by the block’s scope and is undefined after the block exits. So, for example, the following will not work, assuming line 2 is the first reference to separator within this scope: 1 movies.each do |m| 2 separator = ’=>’ # first assignment is inside a block! 3 puts "#{m.title} #{separator} #{m.rating}" # OK 4 end 5 puts "Separator is #{separator}" # === FAILS!! === In a lexically-scoped language such as Ruby, variables are visible to the scope within which they’re created and to all scopes enclosed by that scope. Because in the above snippet separator is created within the block’s scope, its visibility is limited to that scope. In summary, each is just an instance method on a collection that takes a single argument (a block) and provides elements to that block one at a time. A related use of blocks is operations on collections, a common idiom Ruby borrows from functional programming . For example, to double every element in a collection, we could write: 1 new_collection = do |elt| 2 2 * elt 3 end If the parsing is unambiguous, it is idiomatic to use curly braces to delineate a short (one-line) block rather than do...end : 1 new_collection = { |elt| 2 * elt } So, no for-loops? Although Ruby allows for i in collection , each allows us to take better advantage of duck typing , which we’ll see shortly, to improve code reuse. Ruby has a wide variety of such collection operators; Figure 3.7 lists some of the most useful. With some practice, you will automatically start to express operations on collections in terms of these functional idioms rather than in terms of imperative loops. For example, to return a list of all the words
Image of page 118
in some file (that is, tokens consisting entirely of word characters and separated by non-word characters) that begin with a vowel, sorted and without duplicates: 1 # downcase and split are defined in String class 2 words ="file").
Image of page 119
Image of page 120

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