# Enumerable#serially - those nifty functions w/o memory footp

**URL:** <https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504>\
**Category:** Ruby\
**Created:** [May 30, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504 "2007-05-30T15:35:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SonOfLilit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/919ad9/32.png) [@SonOfLilit](https://www.ruby-forum.com/u/SonOfLilit)\
**Post date:** [May 30, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504/1 "2007-05-30T15:35:54Z")

</div>

Hello,

I’ve seen many people write things like:

(1…1000000).to\_a. # anything that is a huge enumerable can work as an  
example  
collect{|x| num\_appearances(x)}.select{|x|  
x.is\_norwegian?}.collect{|x| x.paycheck}.each{|x| p x}

I almost wrote something like that myself today.

The problem, of course, is the huge memory footprint - collect,  
select, collect each creates a new temporary array to store the  
results in.

Here’s a solution to that problem, exchanging it for a bit of speed  
(anything that uses those methods could obviously live with that,  
otherwise another language or method would be used):

module Enumerable  
def serially(&b)  
self.collect{|x| \*([x].instance\_eval(&b)}  
end  
end

Just beware not to use it with sort or any of the “!” methods or with  
sort or sort\_by.

Aur

---

<div class="post-metadata">

**Author:** ![SonOfLilit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/919ad9/32.png) [@SonOfLilit](https://www.ruby-forum.com/u/SonOfLilit)\
**Post date:** [May 30, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504/2 "2007-05-30T15:46:56Z")

</div>

Sorry, found a bug, I’ll need to make it into this:

module Enumerable  
def serially(&b)  
a = []  
self.each{|x| t = ([x].instance\_eval(&b); a \<\< b[0] unless b.empty?}  
# notice, relevance to “it” discussion that’s currently going on,  
just btw  
end  
end

---

<div class="post-metadata">

**Author:** ![SonOfLilit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/919ad9/32.png) [@SonOfLilit](https://www.ruby-forum.com/u/SonOfLilit)\
**Post date:** [May 30, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504/3 "2007-05-30T16:02:08Z")

</div>

On 30.05.2007 15:35, SonOfLilit wrote:

> Hello,
> 
> I’ve seen many people write things like:
> 
> (1…1000000).to\_a. # anything that is a huge enumerable can work as an  
> example  
> collect{|x| num\_appearances(x)}.select{|x|  
> x.is\_norwegian?}.collect{|x| x.paycheck}.each{|x| p x}

Just guessing what all this does, but there is of course the obvious  
solution:

some\_large\_enum.each do |x|  
x = num\_appearances(x)  
p x.paycheck if x.norwegian?  
end

> I almost wrote something like that myself today.
> 
> The problem, of course, is the huge memory footprint - collect,  
> select, collect each creates a new temporary array to store the  
> results in.

Yes, that’s really an issue.

> Just beware not to use it with sort or any of the “!” methods or with  
> sort or sort\_by.

I am not exactly sure I understand what this is supposed to do.´ This  
isn’t even compiling properly.

Regards

robert

---

<div class="post-metadata">

**Author:** ![SonOfLilit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/919ad9/32.png) [@SonOfLilit](https://www.ruby-forum.com/u/SonOfLilit)\
**Post date:** [May 30, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/enumerable-serially-those-nifty-functions-w-o-memory-footp/105504/4 "2007-05-30T16:03:08Z")

</div>

On Wed, May 30, 2007 at 10:46:33PM +0900, SonOfLilit wrote:

> Sorry, found a bug, I’ll need to make it into this:
> 
> module Enumerable  
> def serially(&b)  
> a = []  
> self.each{|x| t = ([x].instance\_eval(&b); a \<\< b[0] unless b.empty?}
> 
> # notice, relevance to “it” discussion that’s currently going on, just
> 
> btw  
> end  
> end

Somehow I don’t think you tested this 🙂

- You have mismatched parentheses
- You assign to t, but never use the value
- you call b twice, once with no arguments, and once with 0 as an  
argument
- b is a block, but you call #empty? on it

Can you give an example of how this is supposed to be used?

Brian.
