From: Martin DeMello Date: 2006-08-07T06:06:10+09:00 Subject: Re: Iterating over an array n element at a time On 8/7/06, Marcelo Alvim wrote: > > Just a tiny little trap you fell into, look at this > > [1,2,3].each_slice(2){|s| p s} > > NoMethodError: undefined method `each_slice' for [1, 2, 3]:Array > > from (irb):1 > > > > but > > > > irb(main):003:0> require 'enumerator' > > => true > > irb(main):004:0> [1,2,3].each_slice(2){|s| p s} > > [1, 2] > > [3] > > > > I have fallen into that one more than once and I still will, it is a little > > bit unclear why Enumerable is not in the core while it is in the core doc. > > But well just a minor hickup. > > Oh, I see! Thanks for the pointer. > > I thought Array mixed that in, so it should be defined for it. But > then again, I'm still getting used to modules, mixins and such. Been > too long in a Java-only world... Array does mix Enumerable in. However, the core Enumerable module doesn't include each_slice; that is provided by the 'enumerator' library, which reopens Enumerable and adds some methods to it. Note that enumerator is in the standard lib rather than in the core. http://www.ruby-doc.org/stdlib/libdoc/enumerator/rdoc/index.html martin