William James
9/17/2007 4:07:00 PM
On Sep 17, 10:35 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: group array elements in groups of two"
> on Tue, 18 Sep 2007 00:18:09 +0900, Emmanuel Oga <oga_emmanuel_...@yahoo.com.ar> writes:
>
> |A better way to do this? :
> |
> |arr= [1, 2, 3, 4, 5, 6, 7, 8]
> |new= []
> |while !arr.empty?
> | elem1, elem2= arr.pop, arr.pop
> | new << [elem2, elem1];
> |end
> |new.reverse!
> |
> |new= [[1, 2], [3, 4], [5, 6], [7, 8]]
>
> require 'enumerator'
> arr= [1, 2, 3, 4, 5, 6, 7, 8]
> new=arr.to_enum(:each_slice, 2).to_a
Nirvana at last! I won a round of golf with
Matz!
YM: .to_enum(:each_slice, 2).to_a
WJ: .enum_slice(2).to_a