[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Suggestion: Enumerable#all?/any? with argument

Farrel Lifson

6/23/2008 9:12:00 PM

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

5 Answers

Farrel Lifson

6/23/2008 9:15:00 PM

0

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

Joel VanderWerf

6/23/2008 9:22:00 PM

0

Farrel Lifson 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Mikael Høilund

6/23/2008 9:25:00 PM

0


On Jun 23, 2008, at 23:12, Farrel Lifson 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 =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?


Robert Klemme

6/24/2008 6:33:00 AM

0

On 23.06.2008 23:12, Farrel Lifson 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 )
> 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

Daniel Brumbaugh Keeney

6/24/2008 7:33:00 AM

0

On Mon, Jun 23, 2008 at 4:12 PM, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> Anyone else see any merit in this? The expression
> [ "one", "two", "three" ].all? String
> just seems quite nice to me.

+1

Daniel Brumbaugh Keeney