deleting an object from an array

M

Mark Volkmann

I assumed incorrectly that Array#delete deleted a specific object,
using eql? (identity). I later discovered that it uses =3D=3D (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 =3D whatever
my_array.delete_if { |element| element.eql?(object_to_delete) }
 
A

ara.t.howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top