On Sun, 5 Feb 2006, Dave Howell wrote:

> On Feb 4, 2006, at 13:10, ara.t.howard / noaa.gov wrote:
>
>> On Sun, 5 Feb 2006, Dave Howell wrote:
>>> 
>>> Can somebody explain to me how I'm supposed to delete a *single* element 
>>> from an array based on equivalence with an object? and get back said array 
>>> with said element deleted?
>>> 
>>> In other words....
>>> 
>>> ar = [1, 2, 3, 8, 15, 15, 8]
>>> 
>>> ar.delete_one_element!(15)
>>> => [1, 2, 3, 8, 15, 8]
>>> 
>>> ar.delete_one_element!(14)
>>> => [1, 2, 3, 8, 15, 15, 8]
>>> 
>>> ar.delete_one_element!(nil)
>>> => [1, 2, 3, 8, 15, 15, 8]
>>> 
>>> I did find so many different ways of not doing this....
>>> 
>
>>   class Array
>>     def remove(*args) replace(self - args) end
>>   end
>> 
>>   a = %w( a b b )
>>   p a.delete_if{|x| x == "b"}
>>   p(a.delete("b") && a)
>>   p a.select{|x| x != "b"}
>>   p a - ["b"]
>>   p a.remove("b", "c")
>> 
>> all return
>>   ["a"]
>
> This is just a list of many other ways to not do what I want, yes? <scratch 
> head>


sorry.  didn't read closely enough:

     harp:~ > cat a.rb
     class Array
       def remove(elem) delete_at(index(elem)) rescue nil; self end
     end

     a = [1, 2, 3, 8, 15, 15, 8]
     p a.remove(15)


     harp:~ > ruby a.rb
     [1, 2, 3, 8, 15, 8]

though i'll say that not raising an error when the element is not found is an
amazingly bad idea that would mask many bugs (imagine an algorithm that should
sometimes remove things but __never__ does)

regards.

-a

-- 
happiness is not something ready-made.  it comes from your own actions.
- h.h. the 14th dali lama