[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array#delete_if: can I get the deleted elements

Raphael Bauduin

10/27/2003 2:48:00 PM

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

10 Answers

Jason Williams

10/27/2003 2:59:00 PM

0

In article <3f9d303e$0$1114$6c56d894@feed0.news.be.easynet.net>,
Raphael Bauduin wrote:
> 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" }

Andrew Walrond

10/27/2003 3:08:00 PM

0

Raphael Bauduin wrote:

>
> 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" }

--
Andrew Walrond

Raphael Bauduin

10/27/2003 3:11:00 PM

0

Jason Williams wrote:
> In article <3f9d303e$0$1114$6c56d894@feed0.news.be.easynet.net>,
> Raphael Bauduin wrote:
>
>>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

Andrew Walrond

10/27/2003 3:11:00 PM

0

Jason Williams wrote:

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

--
Andrew Walrond

Simon Strandgaard

10/27/2003 4:31:00 PM

0

On Mon, 27 Oct 2003 15:11:25 +0000, Andrew Walrond wrote:

> Jason Williams wrote:
>
>>
>> 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 :)

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.

--
Simon Strandgaard



Robert Klemme

10/27/2003 5:24:00 PM

0


"Simon Strandgaard" <qj5nd7l02@sneakemail.com> schrieb im Newsbeitrag
news:pan.2003.10.27.16.30.55.877939@sneakemail.com...
> On Mon, 27 Oct 2003 15:11:25 +0000, Andrew Walrond wrote:
>
> > Jason Williams wrote:
> >
> >>
> >> 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 :)
>
> 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

Jason Williams

10/27/2003 5:29:00 PM

0

In article <bnjkbq$12gjc5$2@ID-52924.news.uni-berlin.de>, Robert Klemme wrote:
>> 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

Also, whytheluckystiff's "What's Shiny and New in Ruby 1.8.0" -

http://whytheluckystiff.net/articles/2003/08/04/ruby...

gabriele renzi

10/27/2003 8:27:00 PM

0

il Mon, 27 Oct 2003 15:11:25 +0000, Andrew Walrond
<andrew@walrond.org> ha scritto::

>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)

Andrew Walrond

10/27/2003 9:02:00 PM

0

gabriele renzi wrote:

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

I've got ri; what's rj ??

--
Andrew Walrond

gabriele renzi

10/27/2003 10:20:00 PM

0

il Mon, 27 Oct 2003 21:02:24 +0000, Andrew Walrond
<andrew@walrond.org> ha scritto::

>gabriele renzi wrote:
>
>>
>> try the latest ri or rj (on rubyforge), this is now my pirmary source
>> of information.. (waiting for pickaxe2)
>
>I've got ri; what's rj ??

http://rubyforge.org/proj...
ypou have to get the cvs yourself..