P
Paul Brannan
Given a singleton class:
irb(main):001:0> class Foo
irb(main):002:1> def self.singleton_class
irb(main):003:2> return class << self; self; end
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> sc = Foo.singleton_class
=> #<Class:Foo>
How can I get class that the singleton class is a singleton class of?
(and is there a name for this?)
class << sc
def un_singleton_class
# ?
end
end
sc.un_singleton_class #=> Foo
I know only of the brute-force solution:
irb(main):011:0> sc = Foo.singleton_class
=> #<Class:Foo>
irb(main):012:0> class Class; def singleton_class; class << self; self; end; end; end
=> nil
irb(main):013:0> ObjectSpace.each_object(Class) { |c| break c if c.singleton_class == sc }
=> Foo
But I was hoping I had overlooked something.
Paul
irb(main):001:0> class Foo
irb(main):002:1> def self.singleton_class
irb(main):003:2> return class << self; self; end
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> sc = Foo.singleton_class
=> #<Class:Foo>
How can I get class that the singleton class is a singleton class of?
(and is there a name for this?)
class << sc
def un_singleton_class
# ?
end
end
sc.un_singleton_class #=> Foo
I know only of the brute-force solution:
irb(main):011:0> sc = Foo.singleton_class
=> #<Class:Foo>
irb(main):012:0> class Class; def singleton_class; class << self; self; end; end; end
=> nil
irb(main):013:0> ObjectSpace.each_object(Class) { |c| break c if c.singleton_class == sc }
=> Foo
But I was hoping I had overlooked something.
Paul