Day 27: A Short Evening of Study

A long day of work and company coming on Thursday night which I need to do some housework to prepare for means I will have a few short nights of study. Today I am focusing on Learn Ruby the Hard Way, Exercise 32: Loops and Arrays.

Here is the code from the lesson:

And the result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

And the study drills:

1. Take a look at how you used (0..5) in the last for-loop. Look up Ruby’s “range operator” (.. and …) online to see what it does.

I worked with ranges at TreeHouse and I feel pretty familiar with with what it does. Read more about ranges here.

2. Change the first for number in the_count to be a more typical .each style loop like the others.

OK. I did this:

and it worked!!!!

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

3. Find the Ruby documentation on arrays and read about them. What other operations can you do besides the push function? Try <<, which is the same as push but is an operator. fruits << x is the same as fruits.push(x).

And it works. It is near the end. I actually thought I screwed up, broke it to make it mess up and redid it. I was shocked I got it right. I remember it from TreeHouse but it was a vague memory.

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex32.rb
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
adding 0 to the list.
adding 1 to the list.
adding 2 to the list.
adding 3 to the list.
adding 4 to the list.
adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

And that is it tonight!