Read more

Top-level constants in BasicObject

Tobias Kraze
April 25, 2016Software engineer at makandra GmbH

If you want to access top-level constants inside a BasicObject class, you need to prefix them with ::.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

This will not work

class Foo < BasicObject
  def bar
    Hash.new
  end
end

Foo.new.bar # => NameError: uninitialized constant Foo::Hash

You need to explicitly write ::Hash.

The reason is that top-level constants are internally attached to Object, so Hash is not in the lookup chain inside a BasicObject.

Posted by Tobias Kraze to makandra dev (2016-04-25 09:50)