From: Thufir Date: 2007-10-27T19:50:20+09:00 Subject: Re: .each do |foo, bar| what does bar do? On Thu, 25 Oct 2007 04:57:50 +0900, David A. Black wrote: > Hashes yield key,value pairs to #each. So you need two block params to > pick them up. > > If you just want the keys, you can use #each_key. Ok, I went and read which helped, if only by confirming the syntax. They give the example: phonebook = { 'Sally Smart' => '555-9999', 'John Doe' => '555-1212', 'J. Random Hacker' => '553-1337' } phonebook['John Doe'] produces '555-1212' To iterate over the hash, use something like the following: phonebook.each {|key, value| puts key + " => " + value} But, what's being iterated through, the keys or the values? To my understanding, each key must be unique and will lookup or map to a specific value. (I'm thinking of the keys as a list, in that there cannot be duplicate keys. There can be only one 'John Doe' in the above to my understanding, but many others could have the same value for the phone number field.) The keys can be iterated through, and that's all that required to get the entire hash map because the values can be looked up from the key field. So, why are both the key and value iterated through? I suppose it's kinda "because". As you said, key value pairs are yielded to #hash, so both key and value must be passed as parameters. Again, though, that doesn't seem strictly required (that both are passed). Why is it required? It certainly seems possible to my mind to pass a key and get back both key and value. It's then clear what's being iterated through: the key field. If both key and field are passed, it's unclear what's being iterated through. thanks, Thufir