DAH, using the 'id' property on an object. Get around ruby

A

Aaron Smith

DAH, I keep running into issues with using the ID property on an object.
What is the correct way to use the ID property on an object, without
pissing Ruby off? In my app I need to be able to use the id property on
any object. Mostly Object/OpenStruct's. I've had a couple things working
but then I run into a situation where it doesn't work correctly. Any
ideas?
 
T

Trans

DAH, I keep running into issues with using the ID property on an object.
What is the correct way to use the ID property on an object, without
pissing Ruby off? In my app I need to be able to use the id property on
any object. Mostly Object/OpenStruct's. I've had a couple things working
but then I run into a situation where it doesn't work correctly. Any
ideas?

Level 1:

obj.object_id

Level 2:

obj.send:)object_id)

Level 3:

Object.instance_method:)object_id).bind(obj).call

Voyage to the depth you require.

T.
 
D

Daniel DeLorme

Trans said:
Level 1:

obj.object_id

Level 1.5:
obj.__id__

Unlike object_id, redefining __id__ causes a warning. I've run into a
case (with rails) where proxy objects forward the object_id method but
not the __id__ method.

Daniel
 
M

Michael Fellinger

DAH, I keep running into issues with using the ID property on an object.
What is the correct way to use the ID property on an object, without
pissing Ruby off? In my app I need to be able to use the id property on
any object. Mostly Object/OpenStruct's. I've had a couple things working
but then I run into a situation where it doesn't work correctly. Any
ideas?

class Object
undef_method:)id)
end

Haven't had any issues with this so far, and it's deprecated anyway.

^ manveru
 
A

Aaron Smith

Michael said:
class Object
undef_method:)id)
end

Haven't had any issues with this so far, and it's deprecated anyway.

^ manveru

That should work perfectly. Thanks.
 

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

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top