From: Sean O'Halpin Date: 2006-07-25T08:19:49+09:00 Subject: Re: Object to which a meta class belongs? On 7/24/06, Erik Veenstra wrote: > 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__ > > ---------------------------------------------------------------- > > > How about this? class Object def singleton_of ObjectSpace.each_object(self) do |obj| return obj end end end Regards, Sean