# RFC - Recursive Array Method call acting on element batches

**URL:** <https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468>\
**Category:** ruby-talk\
**Created:** [24 November 2007 17:35 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468 "2007-11-24T17:35:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rubydeckard](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@Rubydeckard](https://rubytalk.org/u/Rubydeckard)\
**Post date:** [24 November 2007 17:35 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468/1 "2007-11-24T17:35:02Z")

</div>

I work for the Department of Redundancy Department and I'm thinking  
there must be a better, more elegant way to doing the following:

With an array of 200 elements  
Call a method to act on 10 elements at a time  
Repeat until all elements are processed.  
Return the processed array.

Here's my solution to the above:

\_\_BEGIN\_\_

class Array  
&nbsp;&nbsp;def process(method\_obj, batch\_size = 10, fin = [])  
&nbsp;&nbsp;&nbsp;&nbsp;if (this\_batch = self.first(batch\_size)).size \> 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fin.concat method\_obj.call(this\_batch)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(self - this\_batch).process(method\_obj, batch\_size, fin)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fin  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

def munge(ary)  
&nbsp;&nbsp;ary.collect { |e| e.+ 10 }  
end

my\_meth = method(:munge)

ARY = [1,2,3,4,5,6]  
p ARY.process(my\_meth, 2) # run my\_meth against 2 elements at a time

\_\_END\_\_

The above produces: [11, 12, 13, 14, 15, 16]

This is just a trivial example, but, for what I'm actually doing  
there's a limit of 100 items that can be processed by this mysterious  
(dynamic) method, hence the need to "batch" process the array.

Does my solution scream newb? Anyone have a suggestion that would  
"own me with easy"?

Thanks in advance for any comments.

Ben

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [24 November 2007 17:46 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468/2 "2007-11-24T17:46:32Z")

</div>

Hi,

> **···**
>
> In message "Re: RFC - Recursive Array Method call acting on element batches" on Sun, 25 Nov 2007 02:35:02 +0900, rubydeckard@gmail.com writes:
> 
> > I work for the Department of Redundancy Department and I'm thinking  
> > there must be a better, more elegant way to doing the following:
> > 
> > With an array of 200 elements  
> > Call a method to act on 10 elements at a time  
> > Repeat until all elements are processed.  
> > Return the processed array.
> 
> Hmm, how about
> 
> &nbsp;&nbsp;ary = ... # array of 200 elements  
> &nbsp;&nbsp;ary.each\_slice(10).map{|x| ...}
> 
> in 1.9?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [24 November 2007 21:49 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468/3 "2007-11-24T21:49:48Z")

</div>

unknown wrote:

> With an array of 200 elements  
> Call a method to act on 10 elements at a time  
> Repeat until all elements are processed.  
> Return the processed array.

Pre ruby 1.9:

require 'enumerator'

def sum(arr)  
&nbsp;&nbsp;total = 0

&nbsp;&nbsp;arr.each {|elmt| total += elmt}  
&nbsp;&nbsp;#faster than the ponderously slow inject method

&nbsp;&nbsp;total  
end

results =   
arr = Array.new(15) {|i| i}  
p arr

arr.each\_slice(10) do |portion|  
&nbsp;&nbsp;results \<\< sum(portion)  
end

p results

--output:--  
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]  
[45, 60]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rubydeckard](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@Rubydeckard](https://rubytalk.org/u/Rubydeckard)\
**Post date:** [25 November 2007 19:30 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468/4 "2007-11-25T19:30:00Z")

</div>

Thanks for your input guys. That put me on the right track.

&nbsp;&nbsp;def proc\_array(method\_obj, batch\_size = 2)  
&nbsp;&nbsp;&nbsp;&nbsp;result = []  
&nbsp;&nbsp;&nbsp;&nbsp;self.each\_slice(batch\_size) do |batch|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.concat method\_obj.call(batch)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end

That turns out to be twice as fast as the recursive method. The only  
trouble I experienced was getting each\_slice into my class. Turns out  
that there's an error with the ruby core library documentation  
produced by RDoc. #each\_slice is available via the \*standard\* library  
'enumerator.rb', not the core module library Enumerable as is  
documented at [www.ruby-doc.org](http://www.ruby-doc.org). Once I figured that out, all is  
peachy keen.

Thanks,

Ben

---

<div class="post-metadata">

**Author:** ![Eivind\_Eklund](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Eivind\_Eklund](https://rubytalk.org/u/Eivind_Eklund)\
**Post date:** [27 November 2007 13:50 UTC](https://rubytalk.org/t/rfc-recursive-array-method-call-acting-on-element-batches/42468/5 "2007-11-27T13:50:06Z")

</div>

Better:  
&nbsp;&nbsp;def proc\_array(batch\_size = 2, &method\_obj)  
&nbsp;&nbsp;&nbsp;&nbsp;result =   
&nbsp;&nbsp;&nbsp;&nbsp;self.each\_slice(batch\_size) do |batch|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.concat method\_obj.call(batch)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end

Can be called as

...proc\_array do |batch|  
end

or

...proc\_array(2, &method\_obj)

Eivind.

> **···**
>
> On Nov 25, 2007 8:30 PM, \<rubydeckard@gmail.com\> wrote:
> 
> > Thanks for your input guys. That put me on the right track.
> > 
> > &nbsp;&nbsp;def proc\_array(method\_obj, batch\_size = 2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;result =   
> > &nbsp;&nbsp;&nbsp;&nbsp;self.each\_slice(batch\_size) do |batch|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.concat method\_obj.call(batch)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;result  
> > &nbsp;&nbsp;end
