Remember yesterday when I said that we'd be looking at ways of keeping the action moving soon? well, we're almost there, and a key element of that is going to be covered today. You need choice to deal with different situations, you need input to be able to make a choice, but without a looping mechanism, the program will still end when you get to the end o the program. Well, now we're going to look at looping and looping through a special construct called an array (we've dealt with them before, but now we're looking at them specifically and defining them ,too :) ).
In this exercise we are going to take various arrays and we are going to build them, "loop" through them and print out the values. We're going to start off with a specific kind of looping command called the for command (or the for-loop if you prefer).
Loops can be used in various ways, and one of the ways to store the values used in a loop is in an array. To make an array, you can do the following:
hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]
Syntactically, we are using a square bracket to start, or open, the array, then we put individual values into slots using 'single' quote characters for strings, or by themselves if they are digits/integers, and each element is separated by a comma. We close the array with a right bracket. Ruby then takes the entire array, and all of the separated variables, and it assigns it to a single variable name. In this example hairs has the values of brown, blond, and red in three separate slots.
So let's get started and let's see if we can keep track of this:
What You Should See
$ 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
$
Extra Credit
Take a look at how you used the range (0..5). Look up the Range class to understand it.
[ Range allows us to use a shorthand so that we can declare a beginning point and an end point and automatically iterate it. In languages like C we would declare a value, say how high we want the value to iterate, and then use and incrementer. The Range option allows us to do that all by decaring the range like 1..5. In C, this would be the same as a=1; a <= 5; a++) ].
Could you have avoided that for-loop entirely on line 24 and just assigned (0..5) directly to elements?
[ Yes, see below ]
Find the Ruby documentation on arrays and read about them. What other operations can you do to arrays besides push?
[ A whole bunch of them :). Check out http://www.tutorialspoint.com/ruby/ruby_arrays.htm for the descriptions of each of them:
array.abbrev
array.assoc
array.at
array.clear
array.collect
array.collect!
array.compact
array.compact!
array.concat
array.delete
array.delete_at
array.delete_if
array.each
array.each_index
array.empty?
array.eql?
array.fetch
array.fill
array.first
array.flatten
array.flatten!
array.frozen?
array.hash
array.include?
array.index
array.insert
array.inspect
array.join
array.last
array.length
array.map
array.map!
array.nitems
array.pack
array.pop
array.push
array.rassoc
array.reject
array.reject!
array.replace
array.reverse
array.reverse!
array.reverse_each
array.rindex
array.select
array.shift
array.size
array.slice
array.slice!
array.sort
array.sort!
array.to_a
array.to_ary
array.to_s
array.transpose
array.uniq
array.uniq!
array.unshift
array.values_at
array.zip
]
TESTHEAD's TAKEAWAYS:
Loops in Ruby are an example where the language presents itself much more compactly than, say, C or Java. I like the "for x in y" structure and it's cool how it can also take range assignments and apply them. From here, we can combine for loops with other actions and that will allow us to iterate through options. this is definitely helpful if we want to go through something multiple times in rapid succesion. Arrays allow us a way to store lots of information in a single variable space, an reference and manipulate items with a lot of built in functionality.
I've been following these same lessons so it's been good to review your notes as I go through these same ones.
ReplyDeleteDid you notice, though, that on this particular lesson, your output did not match that shown on the site? Mine did not either.
When printing the output of the change array, the single quotes surrounding the coin names are not displayed
I think this is an artifact from when the book was ported from a different language. I'm certain of that, though.
I totally glossed over that, but you are right, the single quotes mentioned do not appear. You may well be right, it could be an artifact of the original Python modules. Only Zed and Rob know for sure :).
ReplyDelete