2008/4/11 said:
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.
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