Here is my work for Exercise 42.
This exercise introduced me to the super method, it invokes the method of the parent class. An interesting thing to take note of is that if you call super() with an empty argument list, no arguments will be passed to the parent class’s method! If you call super with no arguments, super, then it automatically forwards all the arguments from the method which it was called. If you call super(a,b) with a and b arguments, then it will send exactly those arguments to the higher-up method.
1. Research why Ruby added this strange object class, and what that means.
I’m a bit confused by this question, what strange object class? I did some reading and an object is a class is a class… Class is a class. My head hurts, I think I have to get back to this one.
2. Is it possible to use a class like it’s an object?
Yes, because Class inherits from Object. Therefore, every class is an instance of a subclass of Object ( because every class is an instance of Class).
3. Fill out the animals, fish, and people in this exercise with functions that make them do things. See what happens when functions are in a “base class” like Animal versus in say Dog.
The functions in the “base class” get passed down to the child class. If we say the class Animal has a breathe function, then class Dog (which is a subclass of Animal) will also have a breathe function. Animal is the super-class of Dog. The Dog class inherits behaviour from the Animal class. This is Ruby inheritance at work!
I added a say_hello function to the super-class Animal, and then I used the super method to send arguments to the say_hello function (Animal’s say_hello function) from the Cat and Dog classes.
4. Find other people’s code and work out all the is-a and has-a relationships.
Looked through other people’s code on github and tried to interpret the is-a, has-a relationships into English phrases.
5. Make some new relationships that are arrays and hashes so you can also have “has-many” relationships.
I added two instance variables to the class Person, hobbies (an array) and preferences for food (a hash). Class Person has-a name, has-a pet, has-a list of hobbies, and has-a list of food preferences. I also created a describe function, which describes the person’s name, pet (if they have one), and lists their food preferences and hobbies.
I then created a new instance of Person and assigned it to the variable mary. I created a new instance of Cat and set the cat paws to be Mary’s pet. (Ignore the pet satan comment, that was from the original exercise, I have since edited the comment! I’m not sure why Zed Shaw is so against cats. :P) I’ve also added hobbies and dietary preferences for mary.
Finally, I call the function describe on mary and we get the output below.
6. Do you think there’s such thing as an “is-many” relationship? Read about “multiple inheritance,” then avoid it if you can.
Yes, multiple inheritance would be where one class inherits features from more than one parent class. Multiple inheritance can lead to the diamond problem. The diamond problem is called such because of the shape of the diagram of classes. In the diagram, class A is the super-class of both class B and class C. Class D is a sub class of class B and class C. (It inherits from class B, class C and class A.)
The problem occurs when class A has a method, which is inherited by class B and class C. (Let’s say the greeting method puts “Hello there!”) Class B and/or class C override that method. Class B’s greeting method puts “Greetings!” instead. Class C’s greeting method puts “Hello there!” because it inherited the method from class A. Now class D comes along, and it inherits from both class B and class C. What does class D’s greeting method do? Should it inherit from B and puts “Greetings!” or should it inherit from class C and puts “Hello there!” ? This is the diamond problem.
I learned about the super method, which when used in a sub-class’s function, will send that function’s arguments to the function with the same name (higher-level function) in the super-class (parent class). I learned about inheritance (a sub-class will inherit methods from the super-class). I learned about instance variables which are denoted with @ and how these differ from ordinary variables in a method (@ instance variables will retain their values for that instance, whereas function variables have limited scope inside the function) Multiple inheritance is when a class inherits from more than one parent class and leads to the diamond problem. Different languages have different ways of solving the diamond problem.