More specifically:
- When do you need to prefix the scope with
::(like::Foo::Bar) - When is directly referring to a scoped const ok? (just
Foo::Bar) - Is there a good reason why this behavior is so confusing?
EDIT: I am talking about stuff like this
module Foo
THING = 'thing'
module Bar
puts THING
end
end
#=> thing
module Foo::Bar
puts THING
end
#=> NameError: uninitialized constant Foo::Bar::THING
When there’s another constant with the same name in the current namespace.
When there isn’t another constant with the same name in the current namespace. Ie. when that identifier is unambiguous. Similarly, you could just use
Barto aid readability if it was unambiguous.It’s balancing readability and ease of use against specificity. You don’t always want to have to do
::Foo::Bar::Baz::Boo(the globally unique identifier) when you’re deep down in your namespace.