ELABORATION Variable length argument lists A call such as 1minute doesnt have

Elaboration variable length argument lists a call

This preview shows page 116 - 118 out of 517 pages.

ELABORATION: Variable length argument lists A call such as 1.minute doesn’t have any arguments—the only thing that matters is the receiver, 1 . So when the call is redispatched in line 5 of method_missing , we don’t need to pass any of the arguments that were collected in *args . The asterisk is how Ruby deals with variable length argument lists: *args will be an array of any arguments passed to the original method, and if no arguments were passed it will be an empty array. It would be correct in any case for line 5 to read self.send(name+’s’, *args) if we weren’t sure what the length of the argument list was. Self-Check 3.5.1. In the method_missing example above, why are $ and ˆ necessary in the regular expression match in line 4? (Hint: consider what happens if you omit one of them and call 5.milliseconds or 5.secondary ) Without ˆ to constrain the match to the beginning of the string, a call like 5.millisecond would match, which will cause an error when method_missing tries to redispatch the call as 5.milliseconds . Without $ to constrain the match to the end of the string, a call like 5.secondary would match, which will cause an error when method_missing tries to redispatch the call as 5.secondarys . Self-Check 3.5.2. Why should method_missing always call super if it can’t handle the missing method call itself? It’s possible that one of your ancestor classes intends to handle the call, but you must explicitly “pass the method call up the chain” with super to give the ancestor classes a chance to do so. Self-Check 3.5.3. In Figure 3.6 , is Time.now a class method or an instance method? The fact that its receiver is a class name ( Time ) tells us it’s a class method.
Image of page 116
3.6 Blocks: Iterators, Functional Idioms, and Closures Ruby uses the term block somewhat differently than other languages do. In Ruby, a block is just a method without a name, or an anonymous lambda expression in programming-language terminology. Like a regular named method, it has arguments and can use local variables. Here is a simple example assuming movies is an array of Movie objects as we defined in the previous examples: 1 movies.each do |m| 2 puts "#{m.title} was released in #{m.year}" 3 end The method each is an iterator available in all Ruby classes that are collection-like. each takes one argument—a block—and passes each element of the collection to the block in turn. As you can see, a block is bracketed by do and end ; if the block takes arguments, the argument list is enclosed in | pipe symbols | after the do . The block in this example takes one argument: each time through the block, m is set to the next element of movies . Unlike named methods, a block can also access any variable accessible to the scope in which the block appears. For example: 1 separator = ’=>’ 2 movies.each do |m| 3 puts "#{m.title} #{separator} #{m.year}" 4 end In the above code, the value of separator is visible inside the block, even though the variable was created and assigned outside the block. In contrast, the following would not work, because separator is not visible within
Image of page 117
Image of page 118

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