Today we'll be looking at one of Ruby's primary data structures: the Array class. It's important to remember that Array is in fact a class, much like String or Hash, and not a simple primitive. This means that like any other Ruby object, it has an host of methods built right in. Included in these methods are several iteration methods that will come in useful when we get around to finding the sum of our array.
But before we get to that point, we need to declare an array. Ruby provides several ways to instantiate the Array class. So lets look at them using a BNF:
ArrayDecClass ::= Array.new | Array.new(IntegerSize FillObject) | Array.new(ArrayObject) |
Array.new(IntegerSize) { '|' Index '|' StatementList } | [ ElementList ]
IntegerSize ::= Integer
FillObj ::= , Object | ∅
Index ::= identifier
StatementList ::= Statement StatementList | ∅
ElementList ::= Object | ∅
Note: The vertical bars surrounded by single quotes are part of the Ruby array syntax, not the BNF. They appear in the next BNF as well...so watch out!
As you can see, there are many ways to create an array, with each suited to a certain situation. Today we'll be using the array literal form since it's the quickest way to initialize an array with specific values. You can use it to initialize an array like this: [1, 2, 3].
Using this information, define array0 as an empty array, and array1 as an array containing 9.0, 8.0, 7.0, and 6.0. Use ruby to verify that this much is syntactically correct before proceeding:
ruby average.rb
Now that we have defined an array, let's work on the average() method. Since Ruby has 'smart' arrays that know their size, the average() method needs to receive the array, and nothing else.
Once our method has the array, it needs to see if there's anything in it. This is easy to do with an if statement and the Array class's empty? method (which some will argue is "prettier" than saying if arr.size > 0). You'll want to return 0.0 if the array is empty, which can be done with the return keyword. Ruby does have a return statement, it's just not usually required.
If the array is not empty, then you'll need to return the sum of the array's values divided by the number of values it contains. We can determine the number of values in an array using its size attribute:
anArray.sizeTo compute the array's sum, we will define function sum() to go through each element in the array and add it to a total variable. We could do this in the usual way: using a for loop and the subscript method [ ]. However, Ruby provides a better way that is worth mastering: the Array class's each method. Since this is a pretty important method of the Array class, let's look at it using a BNF:
ArrayEachStmt ::= identifier.each { '|' Item '|' Statement } |
identifier.each do '|' Item '|' StatementList end
Item ::= identifier
When an each message is sent to an array,
the Statement or StatementList is performed for
each item in the array, and those statements
can access the item using the Item block-parameter.
Use a total variable and the each method with an item parameter, to compute the sum of the array. If you first initialize your total variable to zero, the body of your sum() method can be as short as 3 lines. You can test your method by uncommenting the puts statements in the main() function that invoke sum().
Once you can compute the sum of the values in an array, finding the average is as simple as implementing this algorithm:
If the number of values in anArray <= 0,
Return 0.0.
Otherwise,
Return the sum of the values in anArray / the number of values in anArray.
Then test your function by uncommenting the
appropriate statements in the main() method.
When your function is correct, use script to create a file in which you list your program and then run it to demonstrate its correctness.
That concludes the Ruby part of this lab.
If this was your last exercise, return to the lab 07 page and follow the "Turn In" instructions there.
Calvin > CS > 214 > Labs > 07 > Ruby