Forum: Ruby deleting an object from an array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-04-03 14:47
(Received via mailing list)
I assumed incorrectly that Array#delete deleted a specific object,
using eql? (identity). I later discovered that it uses == (same
values). Then I looked for a method to delete using eql? and couldn't
find one. I ended up using the following. Is there a better way?

  obj_to_delete = whatever
  my_array.delete_if { |element| element.eql?(object_to_delete) }
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-03 17:09
(Received via mailing list)
On Mon, 3 Apr 2006, Mark Volkmann wrote:

> I assumed incorrectly that Array#delete deleted a specific object,
> using eql? (identity). I later discovered that it uses == (same
> values). Then I looked for a method to delete using eql? and couldn't
> find one. I ended up using the following. Is there a better way?
>
>  obj_to_delete = whatever
>  my_array.delete_if { |element| element.eql?(object_to_delete) }

probably not, but if you're doing alot of this kind of override you
might to

   class IndentArray < ::Array
     alias_method "==", "eql?"
   end

etc.

-a
This topic is locked and can not be replied to.