From: 7stud -- Date: 2007-10-28T05:43:56+09:00 Subject: Re: .each do |foo, bar| what does bar do? Brian Adkins wrote: > On Oct 27, 7:05 am, "David A. Black" wrote: >> i += 1 >> end >> self >> end >> end > > Or this: > > class Hash > def each > each_key {|key| yield key, self[key] } > end > end > That suffers the same problem as David Black's example. > I know, you meant w/o recourse to each* :) My tests show that each_keys() does not call Hash#each(), so your example seems to use fair means to me: class Hash alias :orig_each :each def each(&block) orig_each(&block) puts "orig each called" end def my_method each_key {|key| yield key, self[key] } end end h = {"a"=>1, "b"=>2} #call original each() method for a hash: h.each do |key, val| print key, " ", val puts end puts #call a method that uses each_key(): h.my_method do |key, val| print key, " ", val puts end --output:-- a 1 b 2 orig each called a 1 b 2 Note that in the last output Hash#each() wasn't called. That example has raised a question of my own. Instead of having to write: def each(&block) orig_each(&block) why can't I relay the block to orig_each() without the second '&', like this def each(&block) orig_each(block) According to pickaxe2, p56, the '&' method converts the specified block to a Proc object and assigns it to the parameter variable 'block'. Why is the second call to '&' required? -- Posted via http://www.ruby-forum.com/.