From: Kevin Ballard Date: 2005-10-19T00:36:58+09:00 Subject: Re: Unit testing an each function Peter Hickman wrote: > Sort of like this (trivial example) > > data = Array.new > data << (1..5) > data << (10..20) > > data.each{|i| puts i} > > 1..5 > 10..20 > > # What I want to work is > > data.each.each{|i| puts i} > > 1 > 2 > 3 > 4 > 5 > 10 > 11 > 12 > 13 > 14 > 15 > 16 > 17 > 18 > 19 > 20 > > # What I have to write is > > data.each{|i| i.each{|j| puts j}} > > No great hardship I know but I actually expected data.each.each{...} to > work. Huh, that's an interesting idea. Lets see if we can get it to work. class EachProxy < Object instance_methods.each { |meth| undef_method(meth) unless meth.to_s =~ /^__/ } def initialize(obj) @obj = obj end def method_missing(meth, *args, &block) @obj.each { |item| item.send meth, *args, &block } end end class Array alias :__each__ :each def each(&blk) if blk __each__(&blk) else EachProxy.new(self) end end end Now your example works fine.