Arrays and Hashes
What good are arrays and hashes?
February 1, 2015
Arrays and hashes are two types of objects in Ruby capable of holding large amounts of data. We will start by examining arrays.
Arrays are collections of Ruby objects. For example, an array “x”, might look like:
x = [2, 5, 200000, 4, 1]
y=[“Jim”, “Tammy”, “Nancy”, “Bob”]
z=[“hello”, 2.003, 4, “word”, z]
There are several different ways to create arrays. One can use…
x = Array.new to create an empty array. Or:
x = [1,3,6,2,1] to create the specified array.
Hashes are similar to arrays, except that each object in a hash also has another object corresponding with it. For example, if one ran a store and wanted to associate each item for sale with a price, one could create a hash:
items = [“8 pack of hot dogs” ==> 4;“1lb bananas” ==>10;“Tootsie Roll” ==>.05]