On 2/4/06, Dave Howell <groups / grandfenwick.net> wrote:
>
> On Feb 4, 2006, at 13:10, Wilson Bilkovich wrote:
>
> > If I'm understanding you properly, this is one way:
> >
> > class Array
> >   def delete_one_element!(value)
> >     return self unless i = self.index(value)
> >     self[i] = nil
> >     self.compact!
> >   end
> > end
> >
>
> You are, but it isn't. I might well have "nil" as a legitimate item in
> the array, and .compact would erase all of them. I definitely want a
> solution that can _never_ reduce the size of my array by more than one
> element.
>

OK.. Here's a safer version:
class Array
  def delete_first(value)
    return self unless i = self.index(value)
    self.delete_at(i)
    self
  end
end

irb(main):013:0> a = [1,2,3,8,15,15,8]
=> [1, 2, 3, 8, 15, 15, 8]
irb(main):014:0> a.delete_first(15)
=> [1, 2, 3, 8, 15, 8]
irb(main):015:0>