In fact it is not a legal name for a method at all since only instance variable

In fact it is not a legal name for a method at all

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

class. In fact, it is not a legal name for a method at all, since only instance variable names and class variable names may begin with @ . In this case, the title getter is an instance method of the Movie class. That means that any object that is an instance of Movie (or of one of Movie ’s subclasses, if there were any) could respond to this method. Lines 9–11 define the instance method title= , which is distinct from the title instance method. Methods whose names end in = are setter or mutator methods, and just as with the getter, we need this method because we cannot write [email protected] = ’La vita e bella’ . However, as line 40 shows, we can write beautiful.title = ’La vita e bella’ . Beware! If you’re used to Java or Python, it’s very easy to think of this syntax as assignment to an attribute , but it is really just a method call like any other, and in fact could be written as beautiful.send(:title=, ’La vita e bella’) . And since it is a method call, it has a return value: in the absence of an explicit return statement, the value returned by a method is just the value of the last expression evaluated in that method. Since in this case the last expression in the method is the assignment @title=new_title and the value of any assignment is its right-hand side, the method happens to return the value of new_title that was passed to it. Unlike Java, which allows attributes as well as getters and setters, Ruby’s data hiding or encapsulation is total: the only access to an instance variables or class variables from outside the class is via method calls. This restriction is one reason that Ruby is considered a more “pure” OO language than Java. But since poetry mode allows us to omit parentheses and write movie.title instead of movie.title() , conciseness need not be sacrificed to achieve this stronger encapsulation. Lines 12–13 define the getter and setter for year , showing that you can use semicolons as well as newlines to separate Ruby statements if you think it looks less cluttered. As we’ll soon see, though, Ruby provides a much more concise way to define getters and setters using metaprogramming. Line 14 is a comment, which in Ruby begins with # and extends to the end of the line.
Image of page 112
Line 15 defines a class variable , or what Java calls a static variable , that defines whether a movie’s year of release is included when its name is printed. Analogously to the setter for title , we need one for include_year= ( lines 16–18 ), but the presence of Movie in the name of the method ( Movie.include_year= ) tells us it’s a class method. Notice we haven’t defined a getter for the class variable; that means the value of this class variable cannot be inspected at all from outside the class. Lines 19–25 define the instance method full_title , which uses the value of @@include_year to decide how to display a movie’s full title. Line 21 shows that the syntax #{} can be used to interpolate (substitute) the value of an expression into a double-quoted string, as with #{self.title} and # {self.year} . More precisely, #{} evaluates the expression enclosed in the braces and calls to_s on the result, asking it to convert itself into a string that can be inserted into the enclosing string. The class
Image of page 113
Image of page 114

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