From: Devin Mullins Date: 2005-09-27T06:05:01+09:00 Subject: Re: How each class splats Robert Klemme wrote: >Devin Mullins wrote: > > >>But I think your best bet is define your own Collection object with >>its own #each. >> >> >I though so, too, but it doesn't help: > > > 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}" } >In other words, it's not sufficient to be Enumerable to automatically be >assigned to individual variables in this scenario. You must have an >Array. Maybe to change this is worth an RCR? > > Definitely not .include? Enumerable, since not all Enumerable methods require finity (finiteness? finition? finality?)*. Maybe against .to_a, though, but its deprecatedness (deprecatedity? deprecatality? deprecation? aha! deprecation!) on Object makes me wonder if we aren't heading down a path matz & friends already scoped out and nixed.. >While I would find this convenient it's really not too hard to access >members explicitely. > > Ditto. Devin. *Point in case: class Fibs extend Enumerable def self.each i, j = 0, 1 loop do yield i i, j = j, i + j end end end #find the first Fibonacci number with two nines in a row Fibs.find { |i| i.to_s =~ /99/ }