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.
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.
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!