In Ruby, how can I access the calling class's constants from a parent class?
Module Foo
  class Base
    def test()
      #how do I access calling class's constants here?
      #ex: CallingClass.SOME_CONST
    end
  end
end
class Bar < Foo::Base
  SOME_CONST = 'test'
end
                This seems to work - it forces constant lookup to scope into the current instance's class
module Foo
  class Base
    def test
      self.class::SOME_CONST
    end
  end
end
class Bar < Foo::Base
  SOME_CONST = 'test'
end
Bar.new.test # => 'test'
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With