Array#delete_if: can I get the deleted elements

R

Raphael Bauduin

Hi,

applying the example found in "ri Array#delete_if":

irb(main):020:0> a = [ "a", "b", "c" ]
=> ["a", "b", "c"]
irb(main):021:0> a.delete_if {|x| x >= "b" }
=> ["a"]
irb(main):022:0> a
=> ["a"]

How do I get the elements that where rejected? (Btw, shouldn't this
method end in a "!" ?)

As illustration, I'm looking for a way similar to the pure fictional
method below:
a = [ "a", "b", "c" ]
a.keep_if {|x| x >= "b" }
=> [ "b", "c" ]

Thanks

Raph
 
J

Jason Williams

Hi,

applying the example found in "ri Array#delete_if":

irb(main):020:0> a = [ "a", "b", "c" ]
=> ["a", "b", "c"]
irb(main):021:0> a.delete_if {|x| x >= "b" }
=> ["a"]
irb(main):022:0> a
=> ["a"]

How do I get the elements that where rejected? (Btw, shouldn't this
method end in a "!" ?)

How about instead;

x,y = a.partition { |x| x >= "b" }

The "!" isn't necessary IMO because "delete" already implies the
destructive in-place effect. The not-in-place version is reject_if.
As illustration, I'm looking for a way similar to the pure fictional
method below:
a = [ "a", "b", "c" ]
a.keep_if {|x| x >= "b" }
=> [ "b", "c" ]

a.select { |x| x >= "b" }
 
A

Andrew Walrond

Raphael said:
As illustration, I'm looking for a way similar to the pure fictional
method below:
a = [ "a", "b", "c" ]
a.keep_if {|x| x >= "b" }
=> [ "b", "c" ]

a = [ "a", "b", "c" ]
b = Array.new
a.each { |x| b.push(x) if x >="b" }
 
R

Raphael Bauduin

Jason said:
Hi,

applying the example found in "ri Array#delete_if":

irb(main):020:0> a = [ "a", "b", "c" ]
=> ["a", "b", "c"]
irb(main):021:0> a.delete_if {|x| x >= "b" }
=> ["a"]
irb(main):022:0> a
=> ["a"]

How do I get the elements that where rejected? (Btw, shouldn't this
method end in a "!" ?)


How about instead;

x,y = a.partition { |x| x >= "b" }

The "!" isn't necessary IMO because "delete" already implies the
destructive in-place effect. The not-in-place version is reject_if.

As illustration, I'm looking for a way similar to the pure fictional
method below:
a = [ "a", "b", "c" ]
a.keep_if {|x| x >= "b" }
=> [ "b", "c" ]


a.select { |x| x >= "b" }

Great!

thanks.

Raph
 
A

Andrew Walrond

Jason said:
x,y = a.partition { |x| x >= "b" }

You learn something every day ;) Time to reread the pickaxe to relearn stuff
I obviously missed the first time :)
 
S

Simon Strandgaard

You learn something every day ;) Time to reread the pickaxe to relearn stuff
I obviously missed the first time :)

Array#partition is unfortunatly not documented in pickaxe...

Take a look at 'array.c' in rubys source and you will find many other
not-yet-documented methods.
 
R

Robert Klemme

Simon Strandgaard said:
Array#partition is unfortunatly not documented in pickaxe...

Take a look at 'array.c' in rubys source and you will find many other
not-yet-documented methods.

With a little bit of guessing this irb can help with this, too:

Array.new.public_methods - Array.superclass.public_methods

robert
 
G

gabriele renzi

il Mon, 27 Oct 2003 15:11:25 +0000, Andrew Walrond
You learn something every day ;) Time to reread the pickaxe to relearn stuff
I obviously missed the first time :)


try the latest ri or rj (on rubyforge), this is now my pirmary source
of information.. (waiting for pickaxe2)
 

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,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top