From: Robert Klemme Date: 2012-05-07T23:18:41+09:00 Subject: Re: Why Enumerator#next does not return more than one value? On Mon, May 7, 2012 at 3:10 PM, Földes László wrote: > If I have an Enumerator which yields elements of a mathematical series > (logistic map), I can use: > > enum.first(10) to get the first 10 value, > enum.next to get the next values, > > but there is no enum.next(10) to get the next 10 values. > I do some trickery like this to achieve the same result (because I need > the result in an Array, as #first returns it): > (0..20).map {(lm.next)} > > Is there a nicer way to do it? (Or did I overlook some existing method > in Enumerable?) Question is: what do you need that for? > At first I thought it is logical that #next should have a parameter so > the #first(10) method could be actually a #rewind + #next(10). If you need chunks of the same size #each_slice is the proper method to use. irb(main):015:0> 10.times.each_slice(3).to_a => [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] Note: there's also #each_cons irb(main):017:0> 10.times.each_cons(7).to_a => [[0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9]] Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/