Assuming the following snippet, I’d expect Foo::const_defined?(:Bar)
to be routed via Foo::const_missing. Instead it resolves to top-level
Bar. Can someone explain why please?
–code–
class Bar
end
module Foo
def self.const_missing(konst)
puts “missing konst #{konst}”
end
end
Foo::Bar
#=> missing konst Bar # Unsurprising behaviour
p Foo::const_defined?(:Bar)
false # Unsurprising behaviour
p Foo::const_get(:Bar)
#=> Bar # Surprising behaviour
non-prefixed constants follow the normal scoping rules:
Thanks for the clarification. Purely out of interest, do you find
this behaviour a bit odd (or am I missing obvious reasons as to why it
is implemented this way)?
non-prefixed constants follow the normal scoping rules:
Thanks for the clarification. Purely out of interest, do you find
this behaviour a bit odd (or am I missing obvious reasons as to why it
is implemented this way)?
Chris
so that
class C
a = Array.new # up scope
end
and
class C
a = const_get(‘Array’).new
end
behave
i agree it can be confusing, but the alternative, requiring all
constants to
be fully scoped, would be an enormous pain in the ass.