From: 7stud -- Date: 2007-10-28T03:13:00+09:00 Subject: Re: .each do |foo, bar| what does bar do? David A. Black wrote: > > 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 A test run: h = {"a"=>1, "b"=>2} h.each do |arr| p arr end --output:-- r5test.rb:2: warning: method redefined; discarding old each r5test.rb:15: warning: multiple values for a block parameter (2 for 1) from r5test.rb:7 ["a", 1] r5test.rb:15: warning: multiple values for a block parameter (2 for 1) from r5test.rb:7 ["b", 2] >7stud -- wrote: > > The each() method for a hash sends an array consisting of a key/value > pair to a block: > > h = {"a"=>1, "b"=>2} > > h.each do |arr| > p arr > end > > --output:-- > ["a", 1] > ["b", 2] > > > The output shows that each() *assigns* an array to the parameter > variable 'arr'. -- Posted via http://www.ruby-forum.com/.