0

In Ruby. How do I refer to a class from within the class << self definition?

module MyModule
  class MyClass
    puts self # returns MyModule::MyClass

    class << self
      puts self # returns #<Class:MyModule::MyClass>
      puts ???
    end
  end
end

How do I get the result MyModule::MyClass from where the ??? is?

Thanks, Arth

Arth
  • 12,789
  • 5
  • 37
  • 69

1 Answers1

1

Until somebody comes up with a more orthodox solution, nesting seems to do the work:

module MyModule
  class MyClass
    class << self
      puts nesting[1] # MyModule::MyClass
    end
  end
end
tokland
  • 66,169
  • 13
  • 144
  • 170
  • Thank you, seems to do the trick! Interested to know if there are any other solutions as well though. – Arth Mar 09 '12 at 15:12