Hi --

On Thu, 13 Feb 2003, Sam Roberts wrote:

>
> I wanted to remove all nils and duplicate values from an array:
>
>   ...
>   @fields.collect{ |field| field.group }.compact!.uniq!
> end
>
> I wanted to modify in place, because the return value of collect() is
> ephemeral, nobody else can have a reference, no need to duplicate it, I
> thought...
>
> But now I have:
>
>   all = @fields.collect{ |field| field.group }.compact!
>   all.uniq!
>   all
> end

Partial answer/info:

Keep in mind that a lot of the ! methods return nil if there's no
change:

  irb(main):002:0> [1,2,3].compact!
  => nil

So if compact! works, it's only because there are some nils there.
It's probably best either to modify in place, or to assign the result
to a variable, but not both.


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav