Suggestion: Enumerable#all?/any? with argument

F

Farrel Lifson

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
 
F

Farrel Lifson

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

Joel VanderWerf

Farrel said:
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.
 
M

Mikael Høilund

[ "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 =3D=3D 3 }

Not so much, but it'd be ridiculous to disallow it. Still love the =20
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!

--=20
# Mikael H=F8ilund
def method_missing(m, a=3D0) a +
m.to_s[/[a-z]+/].size * 2; end
p What is the meaning of life?
 
R

Robert Klemme

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.

+1

robert
 
D

Daniel Brumbaugh Keeney

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

+1

Daniel Brumbaugh Keeney
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,302
Latest member
endevsols

Latest Threads

Top