select!

R

Roger Pack

Is there any reason there's no select!
(and I am familiar with compact! but still...)

C:\>ruby -e 'puts [].methods.grep /reject/'
reject
reject!

C:\>ruby -e 'puts [].methods.grep /select/'
select
 
D

David Masover

Perhaps because `select! x` is as simple as `reject! not x`?

For consistency's sake, I'd still suggest the standard library should provide
something like:

module Enumerable
def select!
reject! {|*args| ! yield(*args) }
end
def reject!
select! {|*args| ! yield(*args) }
end
end

Not exactly that, though -- the obvious problem is that if neither select! no
reject! is overridden, you'd want a NoMethodError instead of a stack overflow.
 
R

Robert Klemme

2009/12/15 Roger Pack said:
Is there any reason there's no select!
(and I am familiar with compact! but still...)

C:\>ruby -e 'puts [].methods.grep /reject/'
reject
reject!

C:\>ruby -e 'puts [].methods.grep /select/'
select

Maybe Matz felt that using "select" to actually _delete_ from an Array
would be considered weird. And indeed, we have #delete, #delete_at
and #delete_if:

irb(main):001:0> Array.instance_methods.grep /delete/
=> [:delete, :delete_at, :delete_if]
irb(main):002:0>

:)

Kind regards

robert
 
R

Roger Pack

Enumerable does not have a reject! since not all Enumerables are
mutable either in general or during enumeration

Yeah this is a feature request only for Array#select!

[Enumerable doesn't have a reject! nor should it have a select!]
Maybe Matz felt that using "select" to actually _delete_ from an Array
would be considered weird. And indeed, we have #delete, #delete_at
and #delete_if:

His words:
Enumerables cannot have bang method, since they are not always
mutable. The reason we don't have Array#select! is that I feel the
word select means picking up elements, not removing non-selecting
elements. But I once felt same way to map! and (English speaking)
people persuaded me, so same thing could happen on select! as well.

So yes but hopefully I can change his mind :)

-r
 
P

Phillip Gawlowski

His words:


So yes but hopefully I can change his mind :)

The second "SELECT * FROM TABLE" deletes the records I want to retrieve
from a DBMS, I agree with you. ;)
 

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
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top