year1998 The notation ab always means call method b on receiver a but year is

Year1998 the notation ab always means call method b

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

movie.year=1998 ? The notation a.b always means “call method b on receiver a ”, but @year is the name of an instance variable, whereas year= is the name of an instance method. Self-Check 3.4.2. Suppose we delete line 12 from Figure 3.4 . What would be the result of executing Movie.new(’Inception’,2011).year ? Ruby would complain that the year method is undefined. 3.5 All Programming is Metaprogramming Since defining simple getters and setters for instance variables is so common, we can make the example more Ruby-like by replacing lines 6–11 with the single line attr_accessor :title and lines 12–13 with attr_accessor :year . attr_accessor is not part of the Ruby language—it’s a regular method call that defines the getters and setters on the fly. That is, attr_accessor :foo defines instance methods foo and foo= that get and set the value of instance variable @foo . The related method attr_reader defines only a getter but no setter, and vice versa for attr_writer . This is an example of metaprogramming —creating code at runtime that defines new methods. In fact, in a sense all Ruby programming is metaprogramming, since even a class definition is not a declaration as it is in Java but actually code that is executed at runtime. Given that this is true, you might wonder whether you can modify a class at runtime. In fact you can, by adding or changing instance methods or class methods, even for Ruby’s built-in classes. For example, Figure 3.6 shows a way to do time arithmetic that takes advantage of the now method in the Time class in the standard Ruby library, which returns the number of seconds since 1/1/1970. 1 # Note: Time.now returns current time as seconds since epoch 2 class Fixnum 3 def seconds ; self ; end 4 def minutes ; self * 60 ; end 5 def hours ; self * 60 * 60 ; end 6 def ago ; Time.now - self ; end 7 def from_now ; Time.now + self ; end 8 end 9 Time.now 10 # => Mon Nov 07 10:18:10 -0800 2011 11 5.minutes.ago 12 # => Mon Nov 07 10:13:15 -0800 2011 13 5.minutes - 4.minutes 14 # => 60 15 3.hours.from_now
Image of page 114
16 # => Mon Nov 07 13:18:15 -0800 2011 Figure 3.6: Doing simple time arithmetic by reopening the Fixnum class. Unix was invented in 1970, so its designers chose to represent time as the number of seconds since midnight (GMT) 1/1/1970, sometimes called the beginning of the epoch . For convenience, a Ruby Time object responds to arithmetic operator methods by operating on this representation if possible, though internally Ruby can represent any time past or future. In this example, we reopened the Fixnum class, a core class that we met earlier, and added six new instance methods to it. Since each of the new methods also returns a fixnum, they can be nicely “chained” to write expressions like 5.minutes.ago . In fact, Rails includes a more complete version of this feature that does comprehensive time calculations. Of course, we cannot write 1.minute.ago since we only defined a method called minutes , not minute . We could define additional methods with singular names that duplicate the functionality of the methods we already have, but that’s not very DRY. Instead, we can take advantage of Ruby’s heavy- duty metaprogramming construct method_missing . If a method call cannot be found in the receiver’s
Image of page 115
Image of page 116

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