E
Erik Veenstra
If a singleton class belongs to an object, it should be
possible to determine to which object it belongs. Is that
possible?
I came up with the code below, but it might not be very fast.
Thanks.
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
class Object
def metaclass
obj = self
class << self
self
end.instance_eval do
instance_eval <<-END
def belongs_to
ObjectSpace._id2ref(#{obj.__id__})
end
END
self
end
end
end
a = "TEST"
p a.__id__
p a.metaclass.belongs_to.__id__
----------------------------------------------------------------
possible to determine to which object it belongs. Is that
possible?
I came up with the code below, but it might not be very fast.
Thanks.
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
class Object
def metaclass
obj = self
class << self
self
end.instance_eval do
instance_eval <<-END
def belongs_to
ObjectSpace._id2ref(#{obj.__id__})
end
END
self
end
end
end
a = "TEST"
p a.__id__
p a.metaclass.belongs_to.__id__
----------------------------------------------------------------