






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







In a statically typed o-o language like Java or C++, you can declare a variable in a superclass, then assign a subclass object to that type: public class Bicycle { protected int gear; public void setGear(int nextGear) { gear = nextGear; } } public class MountainBike extends Bicycle { protected int seatHeight; public void setHeight(int newSeatHeight) { seatHeight = newSeatHeight; } } public class BikeSim { public static void main() { ... Bicycle myBike = new MountainBike(); ... myBike.setGear(3); myBike.setHeight(5); } } Which statement is illegal in the code above? Why? In most dynamically typed o-o languages, including Ruby, that statement would be legal. In Ruby, if a method is defined on an object, the object can respond to the message. It doesn’t matter what class the object is declared as … in fact, the object isn’t declared!
This is called unbounded polymorphism —the polymorphism is not limited by the declared class of the object. In contrast, statically typed o-o languages usually have subtype polymorphism —the compiler checks that the invoked method is defined in the type that the object is declared as. Unbounded polymorphism is related to duck typing , which is discussed in next week’s online lectures.
Modules are a way to group together methods, classes and constants. Modules were introduced to programming languages in the 1970s, to segregate data and operations into different pieces. Ruby’s modules are are similar to namespaces in languages such as C++. What’s a namespace? Of course, whenever you mix namespaces, you have the possibility of name clashes. Say you have a graphics library that contains classes for Window and
the position of the top of the Window and the top of the Border, respectively. An application programmer wants to lay out windows with borders,
What’s the problem here? The window functions can go into one module
Exercise: Select the correct method lookup order.
modules.
class with it?
the module to specific objects (not classes). Comparable A good example of the power of modules is Comparable, from the Ruby library. To use comparable in a class, the class needs to define a method
Once we define this method, we get a lot of useful comparison
free. What is this like in Java?
Exercise: Battle of Minions Composing Modules
any class.
apply an operation to adjacent elements in a set:
Exercise: Use inject to define a factorial method.
method yields each time it encounters a vowel.
Here’s an example of its use:
Exercise: Use yield to return every third element of an Array.
We’ve already seen a few examples of where Ruby programs can discover things about themselves at run time. For example, we have seen calls like
Why do we call this “discovering things about [3.14159] at run time”? Reflection allows program entities to discover things about themselves through introspection. For example, an object can ask what its methods are, and a class can tell what its ancestors are. While Java also provides reflection, it does so much more verbosely than Ruby. The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time. Why is this called meta programming?
Exercise: Test your knowledge of collection operators. Now, time to put it all together and work on a more involved program, a Blackjack game. Exercise: Deck and Player