Introduction to hashes

We have seen that an array is a collection of items which are indexed from 0 up to the length of the array minus 1. To illustrate this, if an array (which we will call simply array[]) has 4 elements in it. The index of the first element in the array is given as array[0] and the index of the fourth (and last) element is given as array[3].

If the array had n items the index of the first element would still be array[0] and the index of the last (or nth) element would be given as n-1.

In some situations, we might want to contrive a situation where the elements are indexed from 1 rather than 0 or we may indeed wish to use a non-numeric index and we can do this with hashes.

Essentially, a hash is a collection of key-value pairs and the syntax is:

	hash = {
		key1 => value1,
		key2 => value2,
		key3 => value3
	}

An example of this might look something like:

	my_hash = { "name" => "Eric",
	"age" => 26,
		"hungry?" => true
	}
	puts my_hash["name"]
	puts my_hash["age"]
	puts my_hash["hungry?"]

The output generated by this code is

	Eric
	26
	true

Note that when we are accessing parts of the hash, we are using syntax which is similar to that which we would use to access an element of an array but in this case, rather than providing an index value for the element we want to access, we are using the key and the corresponding value is returned (and in this case sent to the console as output).

We can also create a new empty hash with a statement such as:

pets = Hash.new

Note that Ruby keywords are case-sensitive and so it is important to remember that Hash must be capitalized.

We can add an element to a hash by providing the hash name, the key and the value as follows:

pets[“Stevie”] = “cat”

Note that in this example, Stevie is the key and cat is the value.

Iterating through an array

We saw previously how we could use each (see under arrays) to iterate through an array. Ruby also provides a shorthand for this.

	numbers[1,2,3,4,5]
	numbers.each {|element| puts element}

Note that element here is not a keyword, this is merely a formal parameter so we could use any word here such as number provided we also use the word number in the command to be executed.

The formal parameter here effectively becomes a reference to each element of the array in successive iterations and that allows us to execute some code using this reference without having to worry about providing an index to make sure we are referencing the correct element in each successive iteration.

The code here has only one statement to be executed in each iteration but it looks as though it might be possible to put more than one statement in the code block.

In fact, this is possible but it changes the syntax somewhat. Rather than using curly braces we use the commands do and end with do going before the formal parameter and this will give us something like:

	languages = ["HTML", "COBOL", "Java", "Ruby", "PHP", "Basic", "C"]
	languages.each do |language|
	puts language
	puts
	end

Note that this syntax didn’t work in the CodeAcademy window but works fine when run through the Ruby interpreter.

image1