"E.-R. Bruecklmeier" <news01 / eric-bruecklmeier.de> schrieb im Newsbeitrag
news:400567BA.3070903 / eric-bruecklmeier.de...
> Robert Klemme schrieb:
>
> >>Is there a method similar to Array.delete which returns the modified
> >>array instead of modifing the array by itself?
> >
>
> > irb(main):010:0> a.reject{|s|s>"b"}
> > => ["a", "b"]
> > irb(main):011:0> a
> > => ["a", "b", "c"]
>
>
> sure, that works. I did is this way:
>
> (a.collect {|x| x if x != "c"}).compact
>
> but delete and delete! would be nicer!

Here are more versions, slightly more efficient if the array is large and
only a small portion is selected:

irb(main):008:0> a=%w{a b c}
=> ["a", "b", "c"]
irb(main):009:0> a.inject([]){|arr,s| arr<<s unless s>"b"; arr}
=> ["a", "b"]
irb(main):010:0> a.select{|s| s<="b"}
=> ["a", "b"]

Personally I prefer the select variant because it clearly explains what's
going on.  I
Ruby is so much fun - especially since the advent of "inject". :-)

Cheers

    robert