# Suggestion: Enumerable#all?/any? with argument

**URL:** <https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240>\
**Category:** Ruby\
**Created:** [June 23, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240 "2008-06-23T23:14:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 23, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/1 "2008-06-23T23:14:42Z")

</div>

I was thinking about Enumerable (one of Ruby’s more powerful modules)  
and I was wondering whether the following proposal to extend  
Enumerable#all?/any? would have any merit. In a nutshell I think any?  
and all? should take one argument which is then applied to each item  
using the === operator. For example:  
[“one”, 2, “three”].all?( String )  
would return false, while  
[1, 100, 1000].any?( 50…150 )  
would return true.

In the case where both an argument and a block is supplied both the  
block and the argument would be applied to the item and the results of  
each operation would be combined with AND. For example:  
[“one”, “two”, “six”].all? ( String ) { |item| item.length == 3 }  
would return true while  
[1, 2, “3”].any? ( String ) { |item| item.to\_i \> 4 }  
would return false.

Anyone else see any merit in this? The expression  
[“one”, “two”, “three”].all? String  
just seems quite nice to me.

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 23, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/2 "2008-06-23T23:17:02Z")

</div>

I should also add that the argument would be optional with any? and  
all? acting as per usual if the method argument is omitted.

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 23, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/3 "2008-06-23T23:24:09Z")

</div>

Farrel L. wrote:

> I was thinking about Enumerable (one of Ruby’s more powerful modules)  
> and I was wondering whether the following proposal to extend  
> Enumerable#all?/any? would have any merit. In a nutshell I think any?  
> and all? should take one argument which is then applied to each item  
> using the === operator. For example:  
> [“one”, 2, “three”].all?( String )

Makes sense, because

[“one”, 2, “three”].grep( String )

is already supported.

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 23, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/4 "2008-06-23T23:27:39Z")

</div>

On Jun 23, 2008, at 23:12, Farrel L. wrote:

> [“one”, 2, “three”].all?( String )

I love it.

> [1, 100, 1000].any?( 50…150 )

I love it.

> [“one”, “two”, “six”].all? ( String ) { |item| item.length == 3 }

Not so much, but it’d be ridiculous to disallow it. Still love the  
general idea.

> Anyone else see any merit in this? The expression  
> [“one”, “two”, “three”].all? String  
> just seems quite nice to me.
> 
> >

Indeed it does.

I love it!

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 24, 2008, 8:35am UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/5 "2008-06-24T08:35:36Z")

</div>

On 23.06.2008 23:12, Farrel L. wrote:

> In the case where both an argument and a block is supplied both the  
> block and the argument would be applied to the item and the results of  
> each operation would be combined with AND. For example:  
> [“one”, “two”, “six”].all? ( String ) { |item| item.length == 3 }  
> would return true while  
> [1, 2, “3”].any? ( String ) { |item| item.to\_i \> 4 }  
> would return false.
> 
> Anyone else see any merit in this? The expression  
> [“one”, “two”, “three”].all? String  
> just seems quite nice to me.

+1

robert

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [June 24, 2008, 9:35am UTC](https://www.ruby-forum.com/t/suggestion-enumerable-all-any-with-argument/141240/6 "2008-06-24T09:35:26Z")

</div>

On Mon, Jun 23, 2008 at 4:12 PM, Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Anyone else see any merit in this? The expression  
> [“one”, “two”, “three”].all? String  
> just seems quite nice to me.

+1

Daniel Brumbaugh K.
