Exercise 41: Learning To Speak Object Oriented

Warning

This is being rewritten based on the Python version, there may be errors.

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.

Word Drills

  • class : Tell Ruby to make a new kind of thing.
  • object : Two meanings: the most basic kind of thing, and any instance of some thing.
  • instance : What you get when you tell Ruby to create a class.
  • def : How you define a function inside a class.
  • @ : Inside the functions in a class, @ is an operator for variables in the instance/object being accessed.
  • inheritance : The concept that one class can inherit traits from another class, much like you and your parents.
  • composition : The concept that a class can be composed of other classes as parts, much like how a car has wheels.
  • attribute : A property classes have that are from composition and are usually variables.
  • is-a : A phrase to say that something inherits from another, as in a Salmon is-a Fish.
  • has-a : A phrase to say that something is composed of other things or has a trait, as in a Salmon has-a mouth.

Alright, take some time to make flash cards for those and memorize them. As usual this won't make too much sense until after you're done with this exercise, but you need to know the base words first.

Phrase Drills

Next I have a list of Ruby code snippets on the left, and the English sentences for them:

  1. class X(Y) : "Make a class named X that is-a Y."
  2. class X(object) def initialize(J) : "class X has-a initialize that takes J parameters."
  3. class X(object) def M(J) : "class X has-a function named M that takes J parameters."
  4. foo = X() : "Set foo to an instance of class X."
  5. foo.M(J) : "From foo get the M function, and call it with parameters J."
  6. 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:

  1. "Make a class named ??? that is-a Y."
  2. "class ??? has-a initialize that takes ??? parameters."
  3. "class ??? has-a function named ??? that takes ??? parameters."
  4. "Set foo to an instance of class ???."
  5. "From foo get the ??? function, and call it with parameters ???."
  6. "From foo 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.

Combined Drills

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:

  1. Take a phrase card and drill it.
  2. Flip it over and read the sentence, and for each word in the sentence that is in your words drills, get that card.
  3. Drill those words for that sentence.
  4. Keep going until you are bored then take a break and do it again.

A Reading Test

I now have a little Ruby hack 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'open-uri'

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
  "class ### < ###\nend" => "Make a class named ### that is-a ###.",
  "class ###\n\tdef initialize(@@@)\n\tend\nend"  => "class ### has-a initialize that takes @@@ parameters.",
  "class ###\n\tdef ***(@@@)\n\tend\nend" => "class ### has-a function named *** that takes @@@ parameters.",
  "*** = ###.new()"  => "Set *** to an instance of class ###.",
  "***.***(@@@)"  => "From *** get the *** function, and call it with parameters @@@.",
  "***.*** = '***'" => "From *** get the *** attribute and set it to '***'."
}

PHRASE_FIRST = ARGV[0] == "english"

open(WORD_URL) {|f| 
  f.each_line {|word| WORDS.push(word.chomp)}
}

def craft_names(rand_words, snippet, pattern, caps=false)
  names = snippet.scan(pattern).map do
    word = rand_words.pop()
    caps ? word.capitalize : word
  end

  return names * 2
end

def craft_params(rand_words, snippet, pattern)
  names = (0...snippet.scan(pattern).length).map do
    param_count = rand(3) + 1
    params = (0...param_count).map {|x| rand_words.pop() }
    params.join(', ')
  end

  return names * 2
end

def convert(snippet, phrase)
  rand_words = WORDS.sort_by {rand}
  class_names = craft_names(rand_words, snippet, /###/, caps=true)
  other_names = craft_names(rand_words, snippet, /\*\*\*/)
  param_names = craft_params(rand_words, snippet, /@@@/)
 
  results = []

  for sentence in [snippet, phrase]
    # fake class names, also copies sentence
    result = sentence.gsub(/###/) {|x| class_names.pop }

    # fake other names
    result.gsub!(/\*\*\*/) {|x| other_names.pop }

    # fake parameter lists
    result.gsub!(/@@@/) {|x| param_names.pop }

    results.push(result)
  end

  return results
end 
 
# keep going until they hit CTRL-D
loop do
  snippets = PHRASES.keys().sort_by {rand}

  for snippet in snippets
    phrase = PHRASES[snippet]
    question, answer = convert(snippet, phrase)

    if PHRASE_FIRST
      question, answer = answer, question
    end

    print question, "\n\n> "

    exit(0) unless STDIN.gets

    puts "\nANSWER:  %s\n\n" % answer
  end
end

Here's an example of me running this and trying to answer the questions as accurately as possible. You can see that I type in the answer I think it is based on the phrases I've given you, and then the script prints out the correct answer. You should get your answers as close as possible.

$ ruby ex41.rb
class Branch
	def initialize(bell, degree, arm)
	end
end

> class Branch has-a initialize that takes bell, degree, and arm parameters.

ANSWER:  class Branch has-a initialize that takes bell, degree, arm parameters.

bat.collar(death, arithmetic)

> From bat get the collar function can call it with death, arithmetic parameters.

ANSWER:  From bat get the collar function, and call it with parameters death, arithmetic.

cannon.carpenter = 'corn'

> From cannon get the carpenter attribute and set it to 'corn'.

ANSWER:  From cannon get the carpenter attribute and set it to 'corn'.

animal = Border.new()

> Set animal equal to an instance of class Border.

ANSWER:  Set animal to an instance of class Border.

class Bat < Breakfast
end

> ^D
$

Practice English To Code

Next you should run the script with the "english" option so that you drill the inverse operation. Given an English phrase, write the code for it. Here's me doing that too:

 ruby ex41.rb english
Make a class named Brother that is-a Cracker.

> class Brother < Cracker    

ANSWER:  class Brother < Cracker
end

From behavior get the cent function, and call it with parameters dress, board.

> behavior.cent(derss, board)

ANSWER:  behavior.cent(dress, board)

Set basket to an instance of class Cough.

> ^D
$

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 put any other meaning into 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 and this time, 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:

  1. For each class give its name and what other classes it inherits from.
  2. Under that, list every function it has, and the parameters they take.
  3. List all of the attributes it uses on self.
  4. For each attribute, give the class it 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.