Archive for December 1st, 2009

Hashes (Part 2)

December 01st, 2009 | Category: Basics, Sample Code

Top find out what each key in the hash is associated with :

salad['green salad'] => “lettuce and sweet basil”

As with arrays, there are a lot of useful methods in creating them.

salad.empty? (to check if the hash is empty)
salad.size (to check for the number of elements in the hash)
salad.keys (to get all the keys in the hash to create an array)
salad.values (to get the keys in the form of an array but not necessarily in the order they’ve been entered in the hash)

All the above operations are built-in methods that are used for such purposes.

Comments are off for this post