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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
the_count = [1,2,3,4,5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list # in a more traditional style found in other langues for number in the_count puts "This is count #{number}" end # same as above, but in a more Ruby style # this and the next one are the preferred # way Ruby for-loops are written fruits.each do |fruit| puts "A fruit of type: #{fruit}" end # also we can go through mixed lists too # note this is yet another style, exactly like above # but a different syntax (way to write it) change.each {|i| puts "I got #{i}" } # we can also build lists, first start with an empty one elements = [] # then use the range operator to do 0 to 5 counts (0..5).each do |i| puts "adding #{i} to the list." # pushes the i variable on the *end* of the list elements.push(i) end # now we can print them out too elements.each {|i| puts "Element was: #{i}" } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
the_count = [1,2,3,4,5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list # in a more traditional style found in other langues the_count.each do |number| puts "This is count #{number}" end # same as above, but in a more Ruby style # this and the next one are the preferred # way Ruby for-loops are written fruits.each do |fruit| puts "A fruit of type: #{fruit}" end # also we can go through mixed lists too # note this is yet another style, exactly like above # but a different syntax (way to write it) change.each {|i| puts "I got #{i}" } # we can also build lists, first start with an empty one elements = [] # then use the range operator to do 0 to 5 counts (0..5).each do |i| puts "adding #{i} to the list." # pushes the i variable on the *end* of the list elements.push(i) end # now we can print them out too elements.each {|i| puts "Element was: #{i}" } |

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).**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
the_count = [1,2,3,4,5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list # in a more traditional style found in other langues the_count.each do |number| puts "This is count #{number}" end # same as above, but in a more Ruby style # this and the next one are the preferred # way Ruby for-loops are written fruits.each do |fruit| puts "A fruit of type: #{fruit}" end # also we can go through mixed lists too # note this is yet another style, exactly like above # but a different syntax (way to write it) change.each {|i| puts "I got #{i}" } # we can also build lists, first start with an empty one elements = [] # then use the range operator to do 0 to 5 counts (0..5).each do |i| puts "adding #{i} to the list." # pushes the i variable on the *end* of the list elements << (i) end # now we can print them out too elements.each {|i| puts "Element was: #{i}" } |

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!