Chapter 2: Duly Ruby To the Center of the Gem

Congratulations on rising to the status of Newly Ruby! We're going to focus much more on code examples now that you have been properly initiated into the world of Ruby. Ahead, you will be tested by navigating data structures and writing powerful methods for use in your programs. By the end of this lesson you will become Duly Ruby in preparation of your final challenge.

Arrays are ordered lists. Ruby arrays look like this:

['Good', 'Morning', 'Vietnam']
[3, [2, 5], 64]
[1.6, true, 'corundum']

In the first example, we have an empty array. Next we have an array of strings. The third example contains integers as well as the array [2, 5]. Arrays that have other arrays as elements (they're called 2D or multidimensional arrays) can be very powerful for storing information. Finally we have an array containing a float, a boolean, and a string. Because Ruby interprets all the elements of any array as objects, we can have arrays whose elements are of different data types.

Because arrays are ordered, each element in gets an index. The first element gets index 0, the second index 1, and so on. We access elements of the array directly through these indicies. Take a look:

my_array = [3, 4, 7, 3, 6, 0]

print my_array[4]

We can access the nth element of an array by putting the index in square brackets. Above we accessed the element at index 4 which returns the fifth element of my_array, or 6. We also can access elements in a 2D array:

mult_array = [[1, 2], [3, 4], [5, 6]]

print mult_array[1][0]

The first index in mult_array[1][0] indicates which sub-array and the second indicates the element within the sub-array. The above code would output 3. We can add elements to an array:

my_array = my_array << 23

print my_array

Here we used the << syntax to add 23 to the end of my_array. Ruby will then output [3, 4, 7, 3, 6, 0, 23]. We can also change a specific element of an array through assignment and accessing by index:

my_array[2] = 99

print my_array

Here we set the third element of my_array equal to 99. printing out the array will give us [3, 4, 99, 3, 6, 0, 23]. We can also simply concatenate arrays (since in Ruby arrays are objects too) using the + operator:

your_array = [12, 2, 66, 3]
our_array = my_array + your_array

print our_array

Here we merged the newly created your_array with the updated my_array and stored it in our_array. Feel free to take a guess as to what Ruby will output from the above example.

Iterators are just methods that repeatedly execute blocks of code. We'll see later that code blocks are a big deal in Ruby, especially when it comes to writing your own methods. One of the most basic iterators is the for loop:

for var in 1...10
  puts var

We're asking Ruby to puts the variable var for each integer in the range 1 through 10. The above code will output 1, 2, 3, 4, 5, 6, 7, 8, 9 because the ... syntax excludes the highest end of the range. There also is the .. syntax for an inclusive range. Don't forget to put end at the end of your for loops otherwise Ruby won't know where your code block is finished. for loops are pretty good for when you know how many times you want execute a piece of code, but Ruby can do one better. In some instances you want a quick and dirty way to execute code a set number of times, so Ruby contains the extremely helpful (and aptly named) .times method.

12.times {puts "Huzzah!"}

You'll recognize this syntax from the beginning of this tutorial when you were one of the uninitiated. Here Ruby is simply executed whatever block of code is between the curly braces. Ruby treats {} much how it treats end in other loops. Both serve as demarcation for code blocks.

If you aren't sure how many times you will need to execute a block of code, but instead you want to execute code so long as some condition is true, we use while loops.

counter = 0

while counter < 5
  puts counter
  counter += 1

Here we have an incrementing counter (using the incremental assignment += operator) that is determining how long the while loop will run. Ruby also has an until iterator that is a little like a reverse while:

num = 0

until num == 5
  puts num
  num += 1

In this example we use an until to print out the integers 0 through 4(inclusive), just like the while example.

One of the most power iterator methods in Ruby is the .each method. Take a look at the syntax:

array = ['five', 'four', 'three', 'two', 'one']

array.each { |element| puts element }

It is relatively easy to understand how .each works. It cycles through array putting out each element. Notice the code block syntax in this example. Between the {} we see the || with the placeholder name element. We could have been any name between the ||, since it is simply a way to tell Ruby how we'll reference the elements in the array within our code block. We can also iterate through 2D array using .each:

multi_array = [[3, 54], ['Hello ', 'world!'], [true, false]]

multi_array.each {|sub_array| sub_array.each {|var| puts var}}

In this example, we first call .each on the outer array. Then we call .each again on the variable sub_array, thus iterating through the whole 2D array. Did you by any chance notice how our {|var| #code} code block syntax is very concise and pleasant looking? Ruby is able to accomplish in ONE line of code what would take a lesser language 5 or more. One of the biggest benefits to programming in Ruby is simple and elegant looking code, a boon for the weary-eyed programmer.