Learn Ruby the Hard Way – Exercise 38 Doing Things to Arrays

Here is my work for Exercise 38.

Exercise 38 code
Exercise 38 code
Exercise 38 output
Exercise 38 output

Study Drills

1. Take each function that is called, and go through the steps for function calls to translate them to what Ruby does. For example, more_stuff.pop() is pop(more_stuff).

Translated function calls in comments
Translated function calls in comments

There’s a mistake in the screenshot above, the comment on line #5 should be split(ten_things, ‘ ‘) since the function is being called with argument ten_things.

2. Translate these two ways to view the function calls in English. For example, more_stuff.pop() reads as, “Call pop on more_stuff.” Meanwhile, pop(more_stuff) means, “Call pop with argument more_stuff.” Understand how they are really the same thing.
Understood! I translated both ways to views the function calls in English.

Translated into English
Translated into English

3. Go read about “object-oriented programming” online. Confused? I was too. Do not worry. You will learn enough to be dangerous, and you can slowly learn more later.
I found this great resource on Object-oriented programming. Although it also introduces syntax for the Java language, I found its explanations very easy to understand.

4. Read up on what a “class” is in Ruby. Do not read about how other languages use the word “class.” That will only mess you up.
Ruby ‘s class is like a blueprint from which individual objects are created. For example, your_bicycle is an object with states and behaviours. The object your_bicycle is an instance of the class bicycles. 

5. Do not worry if you do not have any idea what I’m talking about. Programmers like to feel smart so they invented object-oriented programming, named it OOP, and then used it way too much. If you think that’s hard, you should try to use “functional programming.”
Whoa whoa whoa, let’s take this one step at a time…

6. Find 10 examples of things in the real world that would fit in an array. Try writing some scripts to work with them.

1) deck of cards
2) address book
3) menu
4) shopping list
5) directions
6) store locations
7) student name list
8) car models
9) people waiting in line
10) appointments

Students in the Peanuts classroom.
Students in the Peanuts classroom.
Say Hi to all the students.
Say Hi to all the students.

Summary
This exercise introduced me to another way of looking at methods called on an object. When looking at code such as array_name.split(‘ ‘), I looked at it as calling the split method on the array_name array. Zed Shaw says to look at it another way, calling the split method with the array_name as an argument. So you should think of how Ruby is working as split(array_name, ‘ ‘)!

First Ruby looks up the array_name variable, then it looks at its functions. It hits “split” and then it looks at all the stuff that array_name says it owns. It finds split and and grabs that to use, then it sees the () and realizes hey this is a function! That is when Ruby calls the split function, and gives it an extra argument: array_name.

Although I’ve done function calls on objects before, I did not realize this process was what was happening. It’ll take some extra repetition while writing code to get this idea to stick!

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