Here are a few nearly built in ways:
irb(main):001:0> a = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> a,b = a.partition {|x| x % 2 == 0}
=> [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
irb(main):003:0> a
=> [2, 4, 6, 8, 10]
irb(main):004:0> b
irb(main):005:0> a = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):006:0> b =
=>
irb(main):007:0> a.delete_if {|x| x % 2 != 0 and b << x}
=> [2, 4, 6, 8, 10]
irb(main):008:0> a
=> [2, 4, 6, 8, 10]
irb(main):009:0> b
=> [1, 3, 5, 7, 9]
irb(main):010:0> a = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):011:0> b =
=>
irb(main):012:0> a.reject! {|x| x % 2 != 0 and b << x}
=> [2, 4, 6, 8, 10]
irb(main):013:0> a
=> [2, 4, 6, 8, 10]
irb(main):014:0> b
=> [1, 3, 5, 7, 9]
Cheers
robert
···
2008/4/11, Saku Ytti <saku@ytti.fi>:
On (2008-04-12 04:54 +0900), David A. Black wrote:
> There's no general principle at stake; I'm just saying that if you
> want a method that behaves like select but performs an in-place
> reject, it's better to invent a name than override Enumerable#reject!.I see what you mean, and I fully agree that changing existing
methods would cause lot of inconvenience. Kinda just wish this
functionality would be built-in (for performnce reasons), and
select!, reject!, delete_if! might be feasible candidates from
different points of view, if there wasn't history as a burden.
Anyhow than you for your insight.
--
use.inject do |as, often| as.you_can - without end