From: Robert Klemme Date: 2008-02-05T03:29:56+09:00 Subject: Re: convert array to array-of-arrays? On 04.02.2008 19:22, Robert Klemme wrote: > On 04.02.2008 17:32, Phlip wrote: >> Robert Klemme wrote: >> >>> irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a >> >> Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how >> to write to_enum in Ruby? I found each_slice, and I know C and ruby.h, >> but my Ruby internals are rusty... PS: In case you wondered: it's pretty easy: $ irb irb(main):001:0> EnumProxy = Struct.new :obj, :args do irb(main):002:1* include Enumerable irb(main):003:1> def each(&b) irb(main):004:2> obj.send(*args,&b) irb(main):005:2> self irb(main):006:2> end irb(main):007:1> end => EnumProxy irb(main):008:0> class Object irb(main):009:1> def to_enum(*a) irb(main):010:2> EnumProxy.new self, a irb(main):011:2> end irb(main):012:1> end => nil irb(main):013:0> %w{foo bar}.to_enum(:each_with_index).each {|*a| p a} ["foo", 0] ["bar", 1] => # irb(main):014:0> %w{foo bar}.to_enum(:each_with_index).find {|a,b| b==1} => ["bar", 1] Kind regards robert