Learn Ruby the Hard Way – Exercise 42 Learning to Speak Object Oriented

Here is my work for Exercise 41.

I made flash cards for the word and phrase drills.

After going through the flash cards, I typed up the code for Exercise 41 and then spent some time figuring out what was going on. Once I understood the code, I added comments for my own reference, then ran oop_test.rb!

You are presented with a snippet of code, then you can type your own interpretation as input. After you enter your input, the answer is printed to the screen.

Practicing interpreting the snippets of code to phrases
Practicing interpreting the snippets of code to phrases

Study Drills
For these Study Drills, I again turned to Ruby Warrior’s code. I looked at this ruby file for the floor.

1. For each class give its name and what other classes it inherits from.
There is only one class in this file named Floor.

2. Under that, list every function it has and the parameters they take.
Here are the functions it has. To put it into the English phrases I’ve been practicing, Class Floor has-a:

initialize function that takes no parameters
add function that takes unit, x, y, direction parameters
place_stairs function that takes x, y parameters
units function that takes no parameters
other_units function that takes no parameters
get function that takes x,y parameters
space function that takes x, y parameters
out_of_bounds? function that takes x, y parameters
character function that takes no parameters
unique_units function that takes no parameters

3. List all of the attributes it uses on its self.
The attributes it uses on its self are width, height, grid.

4. For each attribute, give the class this attribute is.
All the attributes have the class Floor.

Summary
Okay, maybe that wasn’t such a great file to practice with because it only had one class! I’m still trying to wrap my head around variables, instance variables, and class variables. Sometimes trying to understand a concept is frustrating because I tend to think I’m not understanding what should be a simple concept. But what “should be simple” anyways?

This exercise gives you lots of practice in interpreting snippets of object oriented code into English phrases. I found reviewing the “flashcards” produced by the oop_test.rb code is helpful in looking past the English words and focusing on the structure of the code instead. It’s easy to get hung up on the definitions of the words and forget they are just names!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s