I think it is simple:
ObjectSpace.each_object(As).first
# => #<A:0x000000029a7c50>
質問
Suppose the following code:
class A
end
a = A.new
As = class << a
self
end
# or:
# As = a.singleton_class
Is there some way to get a
from As
?
解決 2
I think it is simple:
ObjectSpace.each_object(As).first
# => #<A:0x000000029a7c50>
他のヒント
Here's a trick for you:
class A
end
a = A.new
As = a.singleton_class
a2 = ObjectSpace.each_object.select{|obj| obj.singleton_class == As}.first
a2 == a # => true
This is just educated guessification on my part so YMMV, but I think "a" needs to be defined as a class variable (ie. "@@a = A.new") and then you would have the appropriate accessor method to return the class instance.
That being said, however, have you considered using the Singleton module (assuming you're on 1.9.3)?