Select!

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

On Dec 15, 2009, at 10:35 , Roger P. wrote:

Is there any reason there’s no select!

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

On Tuesday 15 December 2009 02:51:56 pm Ryan D. wrote:

On Dec 15, 2009, at 10:35 , Roger P. wrote:

Is there any reason there’s no select!

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.

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

Anybody in favor of select!
Could comment on
http://redmine.ruby-lang.org/issues/show/2515

-r

On Tue, Dec 22, 2009 at 9:54 AM, Roger P. [email protected]
wrote:

select! {|*args| ! yield(*args) }

end
end

-1

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

Also the given suggestion would lead to infinite loops, unless reject!
or select! was overridden in the including class.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

2009/12/15 Roger P. [email protected]:

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>

:slight_smile:

Kind regards

robert

On 22.12.2009 17:29, Roger P. wrote:

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 :slight_smile:

The second “SELECT * FROM TABLE” deletes the records I want to retrieve
from a DBMS, I agree with you. :wink:

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 :slight_smile:

-r

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs