From: Robert Klemme Date: 2005-09-27T22:11:42+09:00 Subject: Re: How each class splats Devin Mullins wrote: > Robert Klemme wrote: > >> Devin Mullins wrote: >> >> >>> Oooh... not what I meant. :) I meant something like this: >>> >>> class B >>> attr_reader :i, :v >>> def initialize(i,v) >>> @i, @v = i, v >>> end >>> end >>> >>> class ArrayOfB < DelegateClass(Array) >>> def each >>> super { |b| yield b.i, b.v } >>> end >>> end >>> >>> a = ArrayOfB.new [B.new(:a, 1), B.new(:b, 2)] >>> a.each { |i, v| puts "i = #{i.inspect}, v = #{v.inspect}" } >>> >>> >> Interesting. Why does it work? Which method does the trick here? >> >> > Mrh? ArrayOfB#each does the trick. Can't be: >> class Foo >> def each() yield 1; yield 2 end >> end => nil >> a=[Foo.new, Foo.new] => [#, #] >> a.each {|x,y| puts x,y} # nil # nil => [#, #] It must be something different. Ah! I remember: it's #to_ary: >> class Bar >> def to_ary() [1,2] end >> end => nil >> a=[Bar.new, Bar.new] => [#, #] >> a.each {|x,y| puts x,y} 1 2 1 2 => [#, #] I *knew* it had to be something simple. So Trans, that's the solution! > You understand DelegateClass, no? > (Probably much better than I do...) I know and understand it. I dunno whether better or worse than you. :-) Kind regards robert