# Enumerable#select used to return actual values

**URL:** <https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429>\
**Category:** ruby-talk\
**Created:** [30 November 2008 03:39 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429 "2008-11-30T03:39:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Austin](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mike\_Austin](https://rubytalk.org/u/Mike_Austin)\
**Post date:** [30 November 2008 03:39 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/1 "2008-11-30T03:39:49Z")

</div>

I'm pretty sure select used to use the actual value of the called block, instead of treating it as just a truthy/falsey boolean. I know this is unconventional, but it's a really nice, compact way of expressing map and filter in one shot, like list comprehension:

(1..10).select { |a| a \* a if a \> 5 }

Was it ever there, or did I just have a dream about it? It's easy to implement... just wondering.

Mike

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [30 November 2008 07:54 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/2 "2008-11-30T07:54:51Z")

</div>

> I'm pretty sure select used to use the actual value of the called block, instead of treating it as just a truthy/falsey boolean.

I am pretty sure it did never. Maybe you have a combination with to\_enum in mind or rather inject?

> I know this is unconventional, but it's a really nice, compact way of expressing map and filter in one shot, like list comprehension:
> 
> (1..10).select { |a| a \* a if a \> 5 }
> 
> Was it ever there, or did I just have a dream about it? It's easy to implement... just wondering.

Maybe 1.9 has a nifty trick, I don't use it that often yet.

As said, inject is always a good candidate for this:

irb(main):005:0\> (1..10).inject() {|r,a| r \<\< (a\*a) if a \> 5; r}  
=\> [36, 49, 64, 81, 100]

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 30.11.2008 04:46, Mike Austin wrote:

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 November 2008 11:45 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/3 "2008-11-30T11:45:42Z")

</div>

I always thought it would be nice if Ruby had a method for this.  
Facets' provides #compact\_map. Perhaps not the sexiest name (once upon  
a time it was called #collect\_if), but it's describes the behavior  
properly.

T.

> **···**
>
> On Nov 29, 10:39 pm, Mike Austin \<"mike[nospam]"@mike-austin.com\> wrote:
> 
> > I'm pretty sure select used to use the actual value of the called block,  
> > instead of treating it as just a truthy/falsey boolean. I know this is  
> > unconventional, but it's a really nice, compact way of expressing map and  
> > filter in one shot, like list comprehension:
> > 
> > (1..10).select { |a| a \* a if a \> 5 }
> > 
> > Was it ever there, or did I just have a dream about it? It's easy to  
> > implement... just wondering.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [2 December 2008 02:00 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/4 "2008-12-02T02:00:59Z")

</div>

..  
# (1..10).select { |a| a \* a if a \> 5 }

the difficulty there is that there is no clear separation bw the selection logic and the mapping logic

# Was it ever there, or did I just have a dream about it?  
# It's easy to implement... just wondering.

it's easy in 1.9..

> p (0..10).select\_with\_map -\>(x){x\<5}, -\>(x){x\*x}

[0, 1, 4, 9, 16]

one could also do partitioning if 3rd block is provided (wc catches the else portion)

> p (0..10).select\_with\_map -\>(x){x\<5}, -\>(x){x\*x}, -\>(x){x+x}

[[0, 1, 4, 9, 16], [10, 12, 14, 16, 18, 20]]

but still i prefer {|x| x\<5} over -\>(x){x\<5}  
or maybe, -\>{|x| x\<5} over -\>(x){x\<5}

> **···**
>
> From: Mike Austin [mailto:"mike[nospam]"@mike-austin.com]

---

<div class="post-metadata">

**Author:** ![Mark\_Thomas](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark\_Thomas](https://rubytalk.org/u/Mark_Thomas)\
**Post date:** [4 December 2008 14:44 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/5 "2008-12-04T14:44:17Z")

</div>

This is what I do:

(1..10).collect{ |a| a \* a if a \> 5 }.compact

The only time this doesn't work is when you want to preserve nils in  
your original array.

-- Mark.

> **···**
>
> On Nov 29, 10:46 pm, Mike Austin \<"mike[nospam]"@mike-austin.com\> wrote:
> 
> > I'm pretty sure select used to use the actual value of the called block,  
> > instead of treating it as just a truthy/falsey boolean. I know this is  
> > unconventional, but it's a really nice, compact way of expressing map and  
> > filter in one shot, like list comprehension:
> > 
> > (1..10).select { |a| a \* a if a \> 5 }
> > 
> > Was it ever there, or did I just have a dream about it? It's easy to  
> > implement... just wondering.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 November 2008 10:11 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/6 "2008-11-30T10:11:00Z")

</div>

As Robert was the first to suggest inject I am left with the less  
attractive joices 😉

a.map{ |a| a \> 5 ? a\*a: nil}.compact # does not work for any &blk of course

or

myselection = proc{ |a| a\*a if a \> 5}  
a.select(&myselection).map(&myselection)

or

module Enumerable  
&nbsp;&nbsp;&nbsp;def map\_selection &blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select( &blk ).map( &blk )  
&nbsp;&nbsp;&nbsp;end  
end

HTH  
Robert

> **···**
>
> On Sun, Nov 30, 2008 at 8:54 AM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 30.11.2008 04:46, Mike Austin wrote:
> > 
> > > I'm pretty sure select used to use the actual value of the called block,  
> > > instead of treating it as just a truthy/falsey boolean.
> > 
> > I am pretty sure it did never. Maybe you have a combination with to\_enum in  
> > mind or rather inject?
> > 
> > > I know this is unconventional, but it's a really nice, compact way of  
> > > expressing map and filter in one shot, like list comprehension:
> > > 
> > > (1..10).select { |a| a \* a if a \> 5 }
> > > 
> > > Was it ever there, or did I just have a dream about it? It's easy to  
> > > implement... just wondering.
> > 
> > Maybe 1.9 has a nifty trick, I don't use it that often yet.
> > 
> > As said, inject is always a good candidate for this:
> > 
> > irb(main):005:0\> (1..10).inject() {|r,a| r \<\< (a\*a) if a \> 5; r}
> 
> --  
> Ne baisse jamais la tête, tu ne verrais plus les étoiles.
> 
> Robert Dober 😉

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 November 2008 10:47 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/7 "2008-11-30T10:47:13Z")

</div>

Hi --

> **···**
>
> On Sun, 30 Nov 2008, Robert Klemme wrote:
> 
> > On 30.11.2008 04:46, Mike Austin wrote:
> > 
> > > I'm pretty sure select used to use the actual value of the called block, instead of treating it as just a truthy/falsey boolean.
> > 
> > I am pretty sure it did never. Maybe you have a combination with to\_enum in mind or rather inject?
> > 
> > > I know this is unconventional, but it's a really nice, compact way of expressing map and filter in one shot, like list comprehension:
> > > 
> > > (1..10).select { |a| a \* a if a \> 5 }
> > > 
> > > Was it ever there, or did I just have a dream about it? It's easy to implement... just wondering.
> > 
> > Maybe 1.9 has a nifty trick, I don't use it that often yet.
> 
> (0..10).each\_with\_object() {|e,a| a \<\< (e \* e) if e \> 5 }
> 
> Basically inject with a sticky accumulator.
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> INTRO TO RAILS (Jan 12-15) | ADVANCING WITH RAILS (Jan 19-22) \*  
> Both in Fort Lauderdale, FL \* Co-taught with Patrick Ewing  
> See [http://www.rubypal.com](http://www.rubypal.com) for details  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 November 2008 16:47 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/8 "2008-11-30T16:47:11Z")

</div>

Oh, I remember why it is called #compact\_map now, rather then some  
more concise name. And I've been meaning to bring it up again. Why not  
let #compact itself take a block to do this?

T.

> **···**
>
> On Nov 30, 6:45 am, Trans \<transf...@gmail.com\> wrote:
> 
> > On Nov 29, 10:39 pm, Mike Austin \<"mike[nospam]"@mike-austin.com\> \> wrote:
> > 
> > \> I'm pretty sure select used to use the actual value of the called block,  
> > \> instead of treating it as just a truthy/falsey boolean. I know this is  
> > \> unconventional, but it's a really nice, compact way of expressing map and  
> > \> filter in one shot, like list comprehension:
> > 
> > \> (1..10).select { |a| a \* a if a \> 5 }
> > 
> > \> Was it ever there, or did I just have a dream about it? It's easy to  
> > \> implement... just wondering.
> > 
> > I always thought it would be nice if Ruby had a method for this.  
> > Facets' provides #compact\_map. Perhaps not the sexiest name (once upon  
> > a time it was called #collect\_if), but it's describes the behavior  
> > properly.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [2 December 2008 08:59 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/9 "2008-12-02T08:59:19Z")

</div>

Hi --

> **···**
>
> On Tue, 2 Dec 2008, Peña, Botp wrote:
> 
> > From: Mike Austin [mailto:"mike[nospam]"@mike-austin.com]  
> > ..  
> > # (1..10).select { |a| a \* a if a \> 5 }
> > 
> > the difficulty there is that there is no clear separation bw the selection logic and the mapping logic
> > 
> > # Was it ever there, or did I just have a dream about it?  
> > # It's easy to implement... just wondering.
> > 
> > it's easy in 1.9..
> > 
> > > p (0..10).select\_with\_map -\>(x){x\<5}, -\>(x){x\*x}
> > 
> > [0, 1, 4, 9, 16]
> 
> I don't see that method in 1.9. Which version are you using?
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL  
> See [http://www.rubypal.com](http://www.rubypal.com) for details  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![Mike\_Austin](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mike\_Austin](https://rubytalk.org/u/Mike_Austin)\
**Post date:** [2 December 2008 10:44 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/10 "2008-12-02T10:44:32Z")

</div>

Peña wrote:

> From: Mike Austin [mailto:"mike[nospam]"@mike-austin.com] .  
> # (1..10).select { |a| a \* a if a \> 5 }
> 
> the difficulty there is that there is no clear separation bw the selection logic and the mapping logic

that's... the idea.

> # Was it ever there, or did I just have a dream about it? # It's easy to implement... just wondering.
> 
> it's easy in 1.9..
> 
> > p (0..10).select\_with\_map -\>(x){x\<5}, -\>(x){x\*x}
> 
> [0, 1, 4, 9, 16]

The new syntax looks like a function got run over by a lambda. Twice.  
I guess it wasn't possible to make them look like lambdas?

> one could also do partitioning if 3rd block is provided (wc catches the else portion)
> 
> > p (0..10).select\_with\_map -\>(x){x\<5}, -\>(x){x\*x}, -\>(x){x+x}
> 
> [[0, 1, 4, 9, 16], [10, 12, 14, 16, 18, 20]]
> 
> but still i prefer {|x| x\<5} over -\>(x){x\<5}  
> or maybe, -\>{|x| x\<5} over -\>(x){x\<5}

I wouldn't mind "&{|x| x \< 5}" - shorthand for "&lambda {|x| x \< 5}" which you can pass to functions that require a block:

(1..10).map &lambda {|n| n \* n}

Mike

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [4 December 2008 15:47 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/11 "2008-12-04T15:47:45Z")

</div>

> \> I'm pretty sure select used to use the actual value of the called block,  
> \> instead of treating it as just a truthy/falsey boolean. I know this is  
> \> unconventional, but it's a really nice, compact way of expressing map and  
> \> filter in one shot, like list comprehension:
> 
> \> (1..10).select { |a| a \* a if a \> 5 }
> 
> \> Was it ever there, or did I just have a dream about it? It's easy to  
> \> implement... just wondering.
> 
> This is what I do:
> 
> (1..10).collect{ |a| a \* a if a \> 5 }.compact
> 
> The only time this doesn't work is when you want to preserve nils in  
> your original array.

And that's exactly what I suggested for #compact, to take a block but  
faster b/c it's one pass.

T.

> **···**
>
> On Dec 4, 9:44 am, Mark Thomas \<m...@thomaszone.com\> wrote:
> 
> > On Nov 29, 10:46 pm, Mike Austin \<"mike[nospam]"@mike-austin.com\> \> wrote:
> 
> > -- Mark.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 November 2008 10:13 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/12 "2008-11-30T10:13:58Z")

</div>

Hi --

> **···**
>
> On Sun, 30 Nov 2008, Robert Dober wrote:
> 
> > On Sun, Nov 30, 2008 at 8:54 AM, Robert Klemme \> \<shortcutter@googlemail.com\> wrote:
> > 
> > > On 30.11.2008 04:46, Mike Austin wrote:
> > > 
> > > > I'm pretty sure select used to use the actual value of the called block,  
> > > > instead of treating it as just a truthy/falsey boolean.
> > > 
> > > I am pretty sure it did never. Maybe you have a combination with to\_enum in  
> > > mind or rather inject?
> > > 
> > > > I know this is unconventional, but it's a really nice, compact way of  
> > > > expressing map and filter in one shot, like list comprehension:
> > > > 
> > > > (1..10).select { |a| a \* a if a \> 5 }
> > > > 
> > > > Was it ever there, or did I just have a dream about it? It's easy to  
> > > > implement... just wondering.
> > > 
> > > Maybe 1.9 has a nifty trick, I don't use it that often yet.
> > > 
> > > As said, inject is always a good candidate for this:
> > > 
> > > irb(main):005:0\> (1..10).inject() {|r,a| r \<\< (a\*a) if a \> 5; r}
> > 
> > As Robert was the first to suggest inject I am left with the less  
> > attractive joices 😉
> > 
> > a.map{ |a| a \> 5 ? a\*a: nil}.compact # does not work for any &blk of course
> > 
> > or
> > 
> > myselection = proc{ |a| a\*a if a \> 5}  
> > a.select(&myselection).map(&myselection)
> > 
> > or
> > 
> > module Enumerable  
> > &nbsp;&nbsp;def map\_selection &blk  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select( &blk ).map( &blk )  
> > &nbsp;&nbsp;end  
> > end
> 
> There's also:
> 
> &nbsp;&nbsp;&nbsp;res =   
> &nbsp;&nbsp;&nbsp;(1..10).each {|e| res \<\< (e \* e) if e \> 5 }  
> &nbsp;&nbsp;&nbsp;res
> 
> No shame in a local variable now and then 🙂
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> INTRO TO RAILS (Jan 12-15) | ADVANCING WITH RAILS (Jan 19-22) \*  
> Both in Fort Lauderdale, FL \* Co-taught with Patrick Ewing  
> See [http://www.rubypal.com](http://www.rubypal.com) for details  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))

---

<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:** [30 November 2008 23:24 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/13 "2008-11-30T23:24:14Z")

</div>

Hi,

> **···**
>
> In message "Re: Enumerable#select used to return actual values" on Sun, 30 Nov 2008 19:47:13 +0900, "David A. Black" \<dblack@rubypal.com\> writes:
> 
> > (0..10).each\_with\_object() {|e,a| a \<\< (e \* e) if e \> 5 }
> > 
> > Basically inject with a sticky accumulator.
> 
> #each\_with\_object is an interesting concept. Maybe we can add it to  
> the Enumerable if we can come up with a better name.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<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:** [4 December 2008 16:07 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/14 "2008-12-04T16:07:23Z")

</div>

Hi,

> **···**
>
> In message "Re: Enumerable#select used to return actual values" on Fri, 5 Dec 2008 00:47:45 +0900, Trans \<transfire@gmail.com\> writes:
> 
> > > (1..10).collect{ |a| a \* a if a \> 5 }.compact
> > > 
> > > The only time this doesn't work is when you want to preserve nils in  
> > > your original array.
> > 
> > And that's exactly what I suggested for #compact, to take a block but  
> > faster b/c it's one pass.
> 
> I don't deny the existence of use case of such method (although I  
> cannot think of any realistic one right now), but its name should not  
> be #compact, since it does far more than compacting the receiver.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [30 November 2008 10:26 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/15 "2008-11-30T10:26:19Z")

</div>

Not at all, and it is often the \*fastest\* solution as, unfortunately,  
#inject is slow.

Robert

> **···**
>
> On Sun, Nov 30, 2008 at 11:13 AM, David A. Black \<dblack@rubypal.com\> wrote:
> 
> > There's also:
> > 
> > res =   
> > (1..10).each {|e| res \<\< (e \* e) if e \> 5 }  
> > res
> > 
> > No shame in a local variable now and then 🙂

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [1 December 2008 00:01 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/16 "2008-12-01T00:01:41Z")

</div>

I take it that #each\_with\_object returns the accumulator, ie.

&nbsp;&nbsp;a = (0..10).each\_with\_object() {|e,a| a \<\< (e \* e) if e \> 5 }

Since in 1.9 the block parameter is local to the block.

As for a better name, maybe #each can just take an argument.

&nbsp;&nbsp;(0..10).each() {|e,a| a \<\< (e \* e) if e \> 5 }

Or there's #accumulate, although the object doesn't necessarily have  
to be used according to that meaning.

Do you remain against the idea of #compact taking a block?

T.

> **···**
>
> On Nov 30, 6:24 pm, Yukihiro Matsumoto \<m...@ruby-lang.org\> wrote:
> 
> > Hi,
> > 
> > In message "Re: Enumerable#select used to return actual values" \> on Sun, 30 Nov 2008 19:47:13 +0900, "David A. Black" \<dbl...@rubypal.com\> writes:
> > 
> > \>(0..10).each\_with\_object() {|e,a| a \<\< (e \* e) if e \> 5 }  
> > \>  
> > \>Basically inject with a sticky accumulator.
> > 
> > #each\_with\_object is an interesting concept. Maybe we can add it to  
> > the Enumerable if we can come up with a better name.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [1 December 2008 00:35 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/17 "2008-12-01T00:35:58Z")

</div>

Maybe the difficulty to find a name points to a problem...  
I was just thinking that this looks really cool

.with\_each( 1..10 ){ |o, e| o \<\< e if e \> 5 }

What do you think, surely adding #with\_each to #Object shall be very  
carefully considered, but I like the syntax, err yes I have already  
said that 😉

Robert

> **···**
>
> On Mon, Dec 1, 2008 at 12:24 AM, Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:
> 
> > #each\_with\_object is an interesting concept. Maybe we can add it to  
> > the Enumerable if we can come up with a better name.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
> 
> --  
> Ne baisse jamais la tête, tu ne verrais plus les étoiles.
> 
> Robert Dober 😉

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [1 December 2008 07:53 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/18 "2008-12-01T07:53:56Z")

</div>

Hi --

> Hi,
> 
> \>(0..10).each\_with\_object() {|e,a| a \<\< (e \* e) if e \> 5 }  
> \>  
> \>Basically inject with a sticky accumulator.
> 
> #each\_with\_object is an interesting concept. Maybe we can add it to  
> the Enumerable if we can come up with a better name.

But it's in Enumerable already, in 1.9.

> > RUBY\_DESCRIPTION

=\> "ruby 1.9.0 (2008-11-12 revision 20227) [i386-darwin9.5.0]"

> > Enumerable.instance\_methods(false).grep(/object/)

=\> [:each\_with\_object]

David

> **···**
>
> On Mon, 1 Dec 2008, Yukihiro Matsumoto wrote:
> 
> > In message "Re: Enumerable#select used to return actual values" \> on Sun, 30 Nov 2008 19:47:13 +0900, "David A. Black" \<dblack@rubypal.com\> writes:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL  
> See [http://www.rubypal.com](http://www.rubypal.com) for details  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [4 December 2008 16:28 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/19 "2008-12-04T16:28:40Z")

</div>

Hmmm  
just a little brainstorming on this  
[1, nil, false].compact --\> [1]  
[1, nil, false].compact\_by{ |x| x.nil?} --\> [1, false]

o = Object::new  
a.collect{ |x| some\_condition(x) ? x : o}.compact\_by{ |x| x==o}

But actually the implementation is \*easy\* 😉  
module Enumerable  
&nbsp;&nbsp;&nbsp;alias\_method :compact\_by, :reject ### 😉  
end

However what about

module Enumerable  
&nbsp;&nbsp;&nbsp;alias\_method :\_\_old\_reject\_\_, :reject  
&nbsp;&nbsp;&nbsp;def reject( \*args, &blk )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise SomeDescriptiveError unless blk.nil? || args.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \_\_old\_reject\_\_( &blk ) if blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_old\_reject\_\_{ |x| args.include? x }  
&nbsp;&nbsp;&nbsp;end  
end

I guess I might put this into Labrador 😉

Cheers  
Robert

or

> **···**
>
> On Thu, Dec 4, 2008 at 5:07 PM, Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:
> 
> > Hi,
> > 
> > In message "Re: Enumerable#select used to return actual values" \> on Fri, 5 Dec 2008 00:47:45 +0900, Trans \<transfire@gmail.com\> writes:
> > 
> > \>\> (1..10).collect{ |a| a \* a if a \> 5 }.compact  
> > \>\>  
> > \>\> The only time this doesn't work is when you want to preserve nils in  
> > \>\> your original array.  
> > \>  
> > \>And that's exactly what I suggested for #compact, to take a block but  
> > \>faster b/c it's one pass.
> > 
> > I don't deny the existence of use case of such method (although I  
> > cannot think of any realistic one right now), but its name should not  
> > be #compact, since it does far more than compacting the receiver.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
> 
> --  
> Ne baisse jamais la tête, tu ne verrais plus les étoiles.
> 
> Robert Dober 😉

---

<div class="post-metadata">

**Author:** ![Marcelo1](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Marcelo1](https://rubytalk.org/u/Marcelo1)\
**Post date:** [4 December 2008 16:47 UTC](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429/20 "2008-12-04T16:47:06Z")

</div>

#transform ?

Take something, transform it into something else.

Marcelo

> **···**
>
> On Thu, Dec 4, 2008 at 10:07 AM, Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:
> 
> > I don't deny the existence of use case of such method (although  
> > I cannot think of any realistic one right now), but its name  
> > should not be #compact, since it does far more than compacting  
> > the receiver.

[Next page](https://rubytalk.org/t/enumerable-select-used-to-return-actual-values/50429.md?page=2)
