# Very simple question about enum.all? and any?

**URL:** <https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213>\
**Category:** Ruby\
**Created:** [January 7, 2008, 5:15am UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213 "2008-01-07T05:15:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2008, 5:15am UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/1 "2008-01-07T05:15:55Z")

</div>

%w{ ant bear cat}.all? {|word| word.length \>= 3} #=\> true  
%w{ ant bear cat}.any? {|word| word.length \>= 3} #=\> true

Both of these return true or false as expected. The question is  
is there an equivalent method that returns the original enum object  
itself (instead of just “true”) if the test is satisfied? And nil or  
false  
if otherwise?

I know code for this can be easily written, but the reason for the  
question is a matter of style; because it would be  
really nice not to have to specify the enum object more than one  
time if the test is satisfied.

For example, in simple english, the “any” test would be: “If any item  
passes the test, then return all of the items”.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2008, 6:41am UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/2 "2008-01-07T06:41:12Z")

</div>

On Sun, 6 Jan 2008, removed\_email\_address@domain.invalid wrote:

> question is a matter of style; because it would be  
> really nice not to have to specify the enum object more than one  
> time if the test is satisfied.
> 
> For example, in simple english, the “any” test would be: “If any item  
> passes the test, then return all of the items”.

%w{ant bear cat}.select{|word| word.length \>= 3}

Aditya

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/3 "2008-01-07T14:33:28Z")

</div>

On Jan 7, 2008 5:15 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> %w{ ant bear cat}.all? {|word| word.length \>= 3} #=\> true  
> %w{ ant bear cat}.any? {|word| word.length \>= 3} #=\> true
> 
> Both of these return true or false as expected. The question is  
> is there an equivalent method that returns the original enum object  
> itself (instead of just “true”) if the test is satisfied? And nil or  
> false  
> if otherwise?  
> For #any? the response to your question is yes, #detect or #find. But  
> see below.  
> For #all? however there is none, you shall define what you want, the  
> first, the last a random element satisfying the block?  
> Does not make too much sense for me, but maybe you could give us a  
> usecase?

> I know code for this can be easily written, but the reason for the  
> question is a matter of style; because it would be  
> really nice not to have to specify the enum object more than one  
> time if the test is satisfied.
> 
> For example, in simple english, the “any” test would be: “If any item  
> passes the test, then return all of the items”.  
> This indeed is just a different case, I myself have enhanced grep to  
> do this in my labrador library, so you can say  
> enum.grep{|e| e.length \> 2 } a common idiom to do this is

enum.map{|e| cond(e) ? : e : nil }.compact

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/4 "2008-01-07T16:38:23Z")

</div>

2008/1/7, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Jan 7, 2008 5:15 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > %w{ ant bear cat}.all? {|word| word.length \>= 3} #=\> true  
> > %w{ ant bear cat}.any? {|word| word.length \>= 3} #=\> true
> > 
> > Both of these return true or false as expected. The question is  
> > is there an equivalent method that returns the original enum object  
> > itself (instead of just “true”) if the test is satisfied? And nil or  
> > false  
> > if otherwise?  
> > For #any? the response to your question is yes, #detect or #find. But see below.

I don’t think so. The OP wanted the enum not the element(s).

> For #all? however there is none, you shall define what you want, the  
> first, the last a random element satisfying the block?

He wants the enum. Like in

def my\_test(enum)  
enum.any? {|x| x.length \>= 3} ? enum : nil  
end

> Does not make too much sense for me, but maybe you could give us a usecase?

That would be good. At the moment I cannot see the advantage of the  
proposed solution because if you wanted to work with the return value  
you would have to store it in a local variable anyway. Otherwise you  
would end up invoking an Enumerable method on nil.

> > I know code for this can be easily written, but the reason for the  
> > question is a matter of style; because it would be  
> > really nice not to have to specify the enum object more than one  
> > time if the test is satisfied.
> > 
> > For example, in simple english, the “any” test would be: “If any item  
> > passes the test, then return all of the items”.  
> > This indeed is just a different case, I myself have enhanced grep to  
> > do this in my labrador library, so you can say  
> > enum.grep{|e| e.length \> 2 } a common idiom to do this is

The bit above can be done with #select - IMHO no need to enhance #grep  
for this.

> enum.map{|e| cond(e) ? : e : nil }.compact

What is the advantage of the bit above over

enum.select {|e| cond(e)}

? Am I missing something?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/5 "2008-01-07T22:53:30Z")

</div>

On Jan 7, 2008 4:37 PM, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > For #any? the response to your question is yes, #detect or #find. But see below.  
> > end
> > 
> > > really nice not to have to specify the enum object more than one  
> > > time if the test is satisfied.
> > > 
> > > For example, in simple english, the “any” test would be: “If any item  
> > > passes the test, then return all of the items”.  
> > > This indeed is just a different case, I myself have enhanced grep to  
> > > do this in my labrador library, so you can say  
> > > enum.grep{|e| e.length \> 2 } a common idiom to do this is
> 
> The bit above can be done with #select - IMHO no need to enhance #grep for this.  
> I am not worthy, I am just posting nonsense, arrgh

> > enum.map{|e| cond(e) ? : e : nil }.compact
> 
> What is the advantage of the bit above over
> 
> enum.select {|e| cond(e)}

You tell me 😉

> ? Am I missing something?  
> No I have been quite stupid…

But one thing really strikes me, if OP wanted the enum  
e.any? {} && e  
e.all? {} && e  
would be what he wants, right? I must be quite confused…

Cheers  
Robert

> Kind regards
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 8, 2008, 7:05am UTC](https://www.ruby-forum.com/t/very-simple-question-about-enum-all-and-any/125213/6 "2008-01-08T07:05:53Z")

</div>

On 07.01.2008 22:50, Robert D. wrote:

> But one thing really strikes me, if OP wanted the enum  
> e.any? {} && e  
> e.all? {} && e  
> would be what he wants, right? I must be quite confused…

No, actually this is better than the ternary operator thingy that I  
suggested above. Umpf! That’s a good example that communities can turn  
up better solutions than individuals. (Although, since only Robert was  
involved…) 🙂

I am still waiting for a proper use case of this though. OP?

Cheers

robert
