Misc #16910
closedBasicObject is resolved in BasicObject
Description
I categorized this as Misc because I do not really know if this is a bug, or perhaps I miss some special rule in the resolution of relative constants.
Basically, I would expect
class C < BasicObject
BasicObject
end
or, even simpler,
class BasicObject
BasicObject
end
to raise NameError
.
Reason would be that top-level constants are stored in Object
, which is not among the ancestors in any of the two examples above.
Is my reasoning flawed or is it a bug? If flawed, which is the correct reasoning?
Updated by fxn (Xavier Noria) almost 4 years ago
Ah, forgot to add that I saw this
BasicObject.constants # => [:BasicObject]
Could it be the case that BasicObject
is special-cased somehow? If so, where in the source code?
Updated by Eregon (Benoit Daloze) almost 4 years ago
As you found, I think it's just a special case that BasicObject has a constant named "BasicObject".
It's defined here in CRuby:
https://github.com/ruby/ruby/blob/b10c9d201222b144df7d63660d1c731af53c4ae2/class.c#L569
And here in TruffleRuby:
https://github.com/oracle/truffleruby/blob/e2680f10e367eaca2069636109918541b682a7a0/src/main/java/org/truffleruby/core/CoreLibrary.java#L813-L814
I'm not sure why there is this exception, since all other constants basically don't work in a subclass of BasicObject with lexical constant lookup, they need ::Const
.
Updated by fxn (Xavier Noria) almost 4 years ago
Ahhh, that explains it. That code says this is intentional and definitely not a bug.
Thanks a lot Benoit!
Updated by fxn (Xavier Noria) almost 4 years ago
This can be closed (can't do it myself apparently).
Updated by jeremyevans0 (Jeremy Evans) almost 4 years ago
- Status changed from Open to Closed
Updated by Eregon (Benoit Daloze) almost 4 years ago
Actually I looked too fast, the correct location is
https://github.com/ruby/ruby/blob/b10c9d201222b144df7d63660d1c731af53c4ae2/class.c#L551-L559
So it's defined as a constant of itself (BasicObject) and not Object because Object is not defined yet it seems.
Those constants could probably be defined later though.
Updated by fxn (Xavier Noria) almost 4 years ago
Ah! I read too fast too :). Appreciate the followup!