From: "David A. Black" Date: 2007-10-25T04:57:50+09:00 Subject: Re: .each do |foo, bar| what does bar do? Hi -- On Thu, 25 Oct 2007, Thufir wrote: > "code_words.each do |real, code| > idea.gsub!( real, code ) > end > You see the each method? The each method is all over in Ruby. It's > available for Arrays, Hashes, even Strings. Here, our code_words > dictionary is kept in a Hash. This each method will hurry through all > the pairs of the Hash, one dangerous word matched with its code word, > handing each pair to the gsub! method for the actual replacement." > > from page 33 of whys-poignant-guide-to-ruby.pdf > > > > Is this similar to nested for statements? I don't think so. In the > first line, why are both "real" and "code" part of the interation? >> From my understanding of a hash, you can iterate through the keys only > and then find the corresponding bit of the hash. > > Why would this fail: > > code_words.each do |real| > idea.gsub!( real, code ) > end > > wouldn't the corresponding code get looked up by during the loop? Or, > how could the above be changed so that it would work? 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. 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!