Suggestion: Enumerable#all?/any? with argument

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

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

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.

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!

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

On Mon, Jun 23, 2008 at 4:12 PM, Farrel L. [email protected]
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.