Day 33: Back to Learn Ruby the Hard Way (Part One)

I am back and learning Ruby the Hard Way. Let’s get to it. I am skipping ahead a bit as Exercise 36 was to work on a RPG game and Exercise 37 was memorizing symbols. I am working on a different project soon to add to my portfolio. I am also adding the symbols to AnkiDroid for study. So, I am doing Exercise 38: Doing Things to Arrays.

Here is the code we are starting with:

and the results:

demosthenes131@rails-tutorial:~/workspace/LRTHW (master) $ ruby ex38.rb
Wait there are not 10 things in that list. Let’s fix that.
Adding Boy
There are 7 items now.
Adding Girl
There are 8 items now.
Adding Banana
There are 9 items now.
Adding Corn
There are 10 items now.
There we go: [“Apples”, “Oranges”, “Crows”, “Telephone”, “Light”, “Sugar”, “Boy”, “Girl”, “Banana”, “Corn”].
Let’s do some things with stuff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone#Light#Sugar

So, in this exercise we are seeing how to add items into the array, as well as count the items inside it.

Study Drill time:

1. 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. and 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.

I am going to combine these. Let’s take a look:

Take the ten things and split them up with a space.

While the list of stuff is not 10, take items and add them until there are 10.

Let’s call things from the array in various ways:

The item at cardinal number 1 is Oranges.
The last item is Corn
Pop the last item of the array off, which is Corn.
Join all the items together using a space and list them here.
List the items at cardinal numbers 3 4 5 and join them with a #.

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 decided to find a simple explanation for now. Take a look here.

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.

This was taught at length over at Treehouse!

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

I enjoy this.

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

Hmm. I mean, a lot of things can be used in an array. I will do one example as I worked with these at length at Treehouse.

and the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW (master) $ ruby studydrill38.rb
The best breed of dog is the Jack Russell Terrier.
Jack Russell Terrier * Poodle * German Shepherd * Collie * Pug

OK, be back a little later.