Exercise 41: Learning to Speak Object-Oriented
In this exercise I'm going to teach you how to speak "object oriented." What I'll do is give you a small set of words with definitions you need to know. Then I'll give you a set of sentences with holes in them that you'll have to understand. Finally, I'm going to give you a large set of exercises that you have to complete to make these sentences solid in your vocabulary.
- Tell Ruby to make a new type of thing.
- Two meanings: the most basic type of thing, and any instance of some thing.
- What you get when you tell Ruby to create a class.
- How you define a function inside a class.
- Inside the functions in a class, @ is a variable for the instance/object being accessed.
- The concept that one class can inherit traits from another class, much like you and your parents.
- The concept that a class can be composed of other classes as parts, much like how a car has wheels.
- A property classes have that are from composition and are usually variables.
- A phrase to say that something inherits from another, as in a "salmon" is-a "fish."
- A phrase to say that something is composed of other things or has a trait, as in "a salmon has-a mouth."
Take some time to make flash cards for these terms and memorize them. As usual this won't make too much sense until after you are finished with this exercise, but you need to know the base words first.
Next I have a list of Ruby code snippets on the left, and the English sentences for them:
- class X < y
- "Make a class named X that is-a Y."
- class X: def initialize(J)
- "class X has-a initialize that takes a J parameter."
- class X: def M(J)
- "class X has-a function named M that takes a J parameter."
- foo = X.new()
- "Set foo to an instance of class X."
- "From foo, get the M function, and call it with parameter J."
- foo.K = Q
- "From foo, get the K attribute, and set it to Q."
In each of these, where you see X, Y, M, J, K, Q, and foo, you can treat those like blank spots. For example, I can also write these sentences as follows:
- "Make a class named ??? that is-a Y."
- "class ??? has-a initialize that takes ??? parameters."
- "class ??? has-a function named ??? that takes ??? parameters."
- "Set ??? to an instance of class ???."
- "From ???, get the ??? function, and call it with parameters ???."
- "From ???, get the ??? attribute, and set it to ???."
Again, write these on some flash cards and drill them. Put the Ruby code snippet on the front and the sentence on the back. You have to be able to say the sentence exactly the same every time whenever you see that form. Not sort of the same, but exactly the same.
The final preparation for you is to combine the words drills with the phrase drills. What I want you to do for this drill is this:
- Take a phrase card and drill it.
- Flip it over and read the sentence, and for each word in the sentence that is in your words drills, get that card.
- Drill those words for that sentence.
- Keep going until you are bored, then take a break and do it again.
A Reading Test
I now have a little Ruby hack script that will drill you on these words you know in an infinite manner. This is a simple script you should be able to figure out, and the only thing it does is use a library called urllib to download a list of words I have. Here's the script, which you should enter into oop_test.rb to work with it:
Run this script and try to translate the "object-oriented phrases" into English translations. You should see that the PHRASES dict has both forms and that you just have to enter the correct one.
Practice English to Code
Next you should run the script with the "english" option so that you drill the inverse operation:
$ ruby oop_test.rb english
Remember that these phrases are using nonsense words. Part of learning to read code well is to stop placing so much meaning on the names used for variables and classes. Too often people will read a word like "Cork" and suddenly get derailed because that word will confuse them about the meaning. In the above example, "Cork" is just an arbitrary name chosen for a class. Don't place any other meaning on it, and instead treat it like the patterns I've given you.
Reading More Code
You are now to go on a new quest to read even more code, to read the phrases you just learned in the code you read. You will look for all the files with classes and then do the following:
- For each class give its name and what other classes it inherits from.
- Under that, list every function it has and the parameters they take.
- List all of the attributes it uses on its self.
- For each attribute, give the class this attribute is.
The goal is to go through real code and start learning to "pattern match" the phrases you just learned against how they're used. If you drill this enough you should start to see these patterns shout at you in the code whereas before they just seemed like vague blank spots you didn't know.
Common Student Questions
- This script is hard to get running!
- By this point you should be able to type this in and get it working. It does have a few little tricks here and there, but there's nothing complex about it. Just do all the things you've learned so far to debug scripts. Type each line in, confirm that it's exactly like mine, and research anything you don't know online.
- It's still too hard!
- You can do this. Take it very slow, character by character if you have to, but type it in exactly and figure out what it does.