
-----------------------------------
wtd
Thu Mar 31, 2005 1:25 pm

Hashes
-----------------------------------
A Hash, or "associative array" can be thought of as very similar to a regular array, except that instead of indexing by numbers we can (in terms of Ruby at least) index by anything.  Most commonly we use strings.

So, if I want to have information about an individual, I could write:

info = ["Bob Smith", 42, "129 55th Ave"]

Now, how to I retrieve the name?  Oh, right.  I need to remember that it's the first element in the array.

name = info[0]

Now I could change the age:

info[1] += 1

But, in these cases I have to go back and look at the original creation of the array to see where each item is.  There has to be a better way.

There is.  Instead of using an array, use a hash.

info = { "name" => "Bob Smith", "age" => 42, "address" => "129 55th Ave" }

Now, let's get the name again.

name = info["name"]

And let's change the age.

info["age"] += 1

Any other questions?  :)

-----------------------------------
Tony
Thu Mar 31, 2005 3:06 pm


-----------------------------------
I was actually hoping for a bit more advanced hash handling.

Such as looping through the elements and adding new hashes to the list.

-----------------------------------
wtd
Thu Mar 31, 2005 3:57 pm


-----------------------------------
Getting all keys:

my_hash = {"foo" => "bar"}
my_hash.keys

Getting all values:

my_hash = {"foo" => "bar"}
my_hash.values

Looping:

my_hash = {"foo" => "bar"}

my_hash.each { |key, value| puts "#{key}: #{value}" }

my_hash.each { |pair| puts "#{pair.first}: #{pair.last}" }

for key, value in my_hash
   puts "#{key}: #{value}"
end

for pair in my_hash
   puts "#{pair.first}: #{pair.last}"   
end

Determining if a hash contains a particular key or value:

my_hash = {"foo" => "bar"}

my_hash.has_key? "foo"

my_hash.has_value? "bar"

Adding to a hash:

my_hash = {"foo" => "bar"}

my_hash["hello"] = "world"

-----------------------------------
wtd
Thu Mar 31, 2005 11:25 pm


-----------------------------------
More.  

Let's start with removing entries from a hash if they meet certain conditions.  Ket's say we want to remove any entry where the value is exactly two times the key.

my_hash = {"a" => "aa", "b" => "bbb", 9 => 19, 4 => 8}
new_hash = my_hash.reject { |key, value| value == key * 2 }

new_hash is now:

{"b" => "bbb", 9 => 19}

Next... ever want the keys and values switched?

my_hash = {"a" => "aa", "b" => "bbb", 9 => 19, 4 => 8}
new_hash = my_hash.invert

And one last trick:

Ever want to retrieve multiple entries into an array?

my_hash = {"a" => "aa", "b" => "bbb", 9 => 19, 4 => 8}
my_array = my_hash.indexes("a", 9)

-----------------------------------
wtd
Fri Apr 01, 2005 11:59 pm


-----------------------------------
Anything else anyone would like to know about Ruby hashes?

-----------------------------------
Tony
Sat Apr 02, 2005 1:06 pm


-----------------------------------
thx wtd, awesome examples as always :)
