You are on page 1of 8

## Exercise 32: Loops and Arrays

You should now be able to do some programs that are much more interesting. If
you have been keeping up, you should realize that now you can combine all the
other things you have learned with if-statements and boolean expressions to
make your programs do smart things.

However, programs also need to do repetitive things very quickly. We are going
to use a for-loop in this exercise to build and print various arrays. When you
do the exercise, you will start to figure out what they are. I won't tell you right
now. You have to figure it out.

Before you can use a for-loop , you need a way to store the results of loops
somewhere. The best way to do this is with arrays . Arrays are exactly what
their name says: a container of things that are organized in order from first to
last. It's not complicated; you just have to learn a new syntax. First, there's how
you make arrays :
https://learnrubythehardway.org/book/ex32.html 1/8

## eyes = ['brown', 'blue', 'green']

weights = [1, 2, 3, 4]

You start the array with the [ (le bracket) which "opens" the array . Then
you put each item you want in the array separated by commas, similar to
function arguments. Lastly, end the array with a ] (right bracket) to indicate
that it's over. Ruby then takes this array and all its contents and assigns them to
the variable.

n Warning

This is where things get tricky for people who can't code. Your brain has
been taught that the world is flat. Remember in the last exercise where
you put if-statements inside if-statements ? That probably made
your brain hurt because most people do not ponder how to "nest"
things inside things. In programming nested structures are all over the
place. You will find functions that call other functions that have if-
statements that have arrays with arrays inside arrays. If you see a

structure like this that you can't figure out, take out a pencil and paper
and break it down manually bit by bit until you understand it.

We now will build some arrays using some for-loops and print them out:

1 the_count = [1, 2, 3, 4, 5]

## 2 fruits = ['apples', 'oranges', 'pears', 'apricots']

https://learnrubythehardway.org/book/ex32.html 2/8

9 end

10

## 13 # way Ruby for-loops are written

14 fruits.each do |fruit|

16 end

17

22

24 elements = []

25

## 26 # then use the range operator to do 0 to 5 counts

27 (0..5).each do |i|

## 29 # pushes the i variable on the *end* of the list

30 elements.push(i)

31 end

32

## 34 elements.each {|i| puts "Element was: #{i}" }

https://learnrubythehardway.org/book/ex32.html 3/8

You should immediately see that Ruby has two kinds of loops that I am calling a
for-loop . In programming the term for-loop just means "a loop that goes

through each thing in an array of things." In Ruby this is both for number in
the_count style, and the more common fruits.each style. You should use the

.each version as it is more reliable and what other Ruby programmers expect

you to write.

n Warning

Ruby programmers are very particular about how their for-loops are
written and will declare you a bad programmer for simply using this
one construct wrong. They went so far as to break the for-each
version of looping so that there are problems with using it, forcing you
to conform to their culture. Heed my warning that you should always
use .each and never for-each for fear of being forever branded bad
and shunned. Yes, it is as ridiculous as it sounds.

## What You Should See

https://learnrubythehardway.org/book/ex32.html 4/8

\$ ruby ex32.rb

This is count 1

This is count 2

This is count 3

This is count 4

This is count 5

I got 1

I got pennies

I got 2

I got dimes

I got 3

I got quarters

## adding 5 to the list.

Element was: 0

Element was: 1

Element was: 2

Element was: 3

Element was: 4

Element was: 5

https://learnrubythehardway.org/book/ex32.html 5/8

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.

## style loop like the others.

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

## Depends on the language and the implementation. In classic terms a list

is very di erent from an array because of how they're implemented. In
Ruby though they call these arrays. In Python they call them lists. Just
call these arrays for now since that's what Ruby calls them.

## The variable is defined by the for-loop when it starts, initializing it to

the current element of the loop iteration each time through.

https://learnrubythehardway.org/book/ex32.html 6/8

Python The Hard Way. Get it now at http://bit.ly/buylmorepthw
(http://bit.ly/buylmorepthw) for \$29.99 before the price goes up!

When you buy directly from the author, Zed A. Shaw, you'll get a
professional quality PDF and hours of HD Video, all DRM-free and yours

\$
29. 99

pecimen/Source+Serif+Pro),

pecimen/Source+Sans+Pro),
RIGHT HERE, VIDEO LECTURES NOT INCLUDED.

https://learnrubythehardway.org/book/ex32.html 7/8