From: "David A. Black" Date: 2007-10-27T20:05:33+09:00 Subject: Re: .each do |foo, bar| what does bar do? Hi -- On Sat, 27 Oct 2007, Thufir wrote: > On Thu, 25 Oct 2007 06:14:12 +0900, Ben Giddings wrote: > >> Because "yield" has two arguments, the block you pass each should have >> two parameters, which it does. If you used this instead: >> >> code_words.each do |foo| >> ... >> end >> >> Foo would be assigned an array containing both things sent by "yield", >> i.e. foo[0] would be the same as real, and foo[1] would be the same as >> code. > > > I don't see what's wrong with having foo[0] and foo[1], but, yes, it's > clearer to instead use foo and bar instead. Ok, that convinces me that > it's good to do it this way, thank you. > > I just can't wrap my mind around *what's* being iterated through. I > guess it's the way that, as you put it, the "yield(current_hash_key, > current_hash_value)" is happening behind the scenes. Yes, that's correct. yield is a keyword that acts like a method call; it can have multiple arguments. Blocks can have multiple parameters, and can therefore be called with multiple arguments. If you were going to write Hash#each in Ruby, without recourse to #each, you could write it like this: class Hash def each keys = self.keys # make a 'keys' local variable i = 0 until i == keys.size key = keys[i] yield(key, self[key]) i += 1 end self end end David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Edison, NJ, November 6-9 * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!