From: "Florian Groß" Date: 2005-09-26T22:55:48+09:00 Subject: Re: How each class splats Trans wrote: > First I'll just ask it there's a way to do this WITHOUT redefining > #each in Array. > > class A > def initialize(i,v) > @i,@v=i,v > end > end > > arrayofA = [ A.new(:a,1), A.new(:b,2) ] > > arrayofA.each { |e| p e } > => # > # > > arrayofA.each { |i,v| p i,v } > => :a > 1 > :b > 2 Try this: > class A > def initialize(i,v) > @i, @v = i, v > end > > def to_a() > [@i, @v] > end > end > > ary = [A.new(:a, 1), A.new(:b, 2)] > ary.each { |a| i, v = *a; p i, v } In theory .each { |(i, v)| ... } should also work, but it seems that Ruby doesn't call .to_a there. (I think it should do so.)