the instance methods class methods and variables in the module become available

The instance methods class methods and variables in

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

, the instance methods, class methods, and variables in the module become available in the class. The collection methods in Figure 3.7 are part of a module called Enumerable that is part of Ruby’s standard library; to mix Enumerable into your own class, just say include Enumerable inside the class definition. Watch out! Because Ruby allows adding and defining methods at runtime, include cannot check whether the module’s contract is fulfilled by the class. As its documentation states, Enumerable requires the class mixing it in to provide an each method, since Enumerable ’s collection methods are implemented in terms of each . Unlike a Java interface, this simple contract is the only requirement for mixing in the module; it doesn’t matter what class you mix it into as long as that class defines the each instance method, and neither the class nor the mix-in have to declare their intentions in advance. For example, the each method in Ruby’s Array class iterates over the array elements, whereas the each method in the IO class iterates over the lines of a file or other I/O stream. Mix-ins thereby allow reusing whole collections of behaviors across classes that are otherwise unrelated. The term “duck typing” is a popular description of this capability, because “if something looks like a duck and quacks like a duck, it might as well be a duck.” That is, from Enumerable ’s point of view, if a class has an each method, it might as well be a collection, thus allowing Enumerable to provide other methods implemented in terms of each . Unlike Java’s Interface , no formal declaration is required for mix-ins; if we invented a new mixin that relied on (say) a class implementing the dereference operator [] , we could then mix it into any such class without otherwise modifying the classes themselves. When Ruby programmers say that some class “quacks like an Array ,” they usually mean that it’s not necessarily an Array nor a descendant of Array , but it responds to most of the same methods as Array
Image of page 121
and can therefore be used wherever an Array would be used. <=> is sometimes called the spaceship operator since some people think it looks like a flying saucer. Because Enumerable can deliver all the methods in Figure 3.7 (and some others) to any class that implements each , all Ruby classes that “quack like a collection” mix in Enumerable for convenience. The methods sort (with no block), max , and min also require that the elements of the collection (not the collection itself) respond to the <=> method, which returns -1, 0, or 1 depending on whether its first argument is less than, equal to, or greater than its second argument. You can still mix in Enumerable even if the collection elements don’t respond to <=> ; you just can’t use sort , max , or min . In contrast, in Java every collection class that implemented the Enumerable interface would have to ensure that its elements could be compared, whether that functionality was required or not.
Image of page 122
Image of page 123

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