Any Array method to delete elements and return them?

Hi, I've an array like this:

  a = [1,2,3,"aaa","bbb",4,"ccc"]

I want to remove from the array all the Strings, but also want these Strings
in a new Array.

Array#delete_if of Array#reject! is not valid because they delete elements but
don't return them, but the modified array:

  a.reject! {|e| e.is_a?(String)}
  => [1, 2, 3, 4]

  a.delete_if {|e| e.is_a?(String)}
  => [1, 2, 3, 4]

I want a method that deletes elements from the array and return the deleted
elements, somethis as:

  a.delete_and_extract_if {|e| e.is_a?(String)}
  => ["aaa", "bbb", "ccc"]

Does such method exist?

PS: Of course I can do it by using Array#collet and Array#reject! in two
different commands, but would like it to be more efficient.

Thanks.

···

--
Iñaki Baz Castillo <ibc@aliax.net>

Iñaki Baz Castillo wrote:

Hi, I've an array like this:

  a = [1,2,3,"aaa","bbb",4,"ccc"]

I want to remove from the array all the Strings, but also want these
Strings
in a new Array.

strings, a = a.partition{|e| e.is_a?(String)}

hth,

Siep

···

--
Posted via http://www.ruby-forum.com/\.

Great!

···

El Viernes, 11 de Diciembre de 2009, Siep Korteling escribió:

Iñaki Baz Castillo wrote:
> Hi, I've an array like this:
>
> a = [1,2,3,"aaa","bbb",4,"ccc"]
>
> I want to remove from the array all the Strings, but also want these
> Strings
> in a new Array.

strings, a = a.partition{|e| e.is_a?(String)}

--
Iñaki Baz Castillo <ibc@aliax.net>