Issue #14148 has been updated by RickHull (Rick Hull).

Subject changed from Ruby 2.5.0-preview1 - NameError on scoped constant after toplevel constant is defined to Longstanding behavior regarding correspondence of toplevel with Object is surprising

I edited the title of this issue.  I reported this initially as 2.5.0 issue, as I discovered it while playing around with the 2.5.0 changes regarding toplevel constant lookup.  Now, this ticket is more about surprising behavior in general, having to do with the "strange" correspondence between toplevel and Object, that I believe could or should be fixed at the design level.  As this is longstanding behavior, I understand if this is a WONTFIX or similar.  Furthermore, lacking detailed knowledge of the history and internals that led to this state of affairs, I will refrain from commenting further unless prompted.

In terms of design, I think that the toplevel should be its own "special" scope, not derived from Object.  I'm not sure how "special" it needs to be.  I think toplevel constants should be something like `toplevel::X` and not correspond to `Object::X`.  naked methods like `def foo` could likewise correspond to a hidden `toplevel#foo` or `main#foo`.  But I don't think I have the level of understanding to make a concrete proposal that would preserve some level of compatibility while meeting the expectations of both new and longtime rubyists.

----------------------------------------
Bug #14148: Longstanding behavior regarding correspondence of toplevel with Object is surprising
https://bugs.ruby-lang.org/issues/14148#change-68146

* Author: RickHull (Rick Hull)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0preview1 (2017-10-10 trunk 60153) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
~~~ ruby
module Kernel
  X = 1
end

puts String::X

X = 2

puts String::X
~~~

~~~
$ ruby test.rb
1
Traceback (most recent call last):
test.rb:9:in `<main>': uninitialized constant String::X (NameError)
Did you mean?  X

$ ruby --version
ruby 2.5.0preview1 (2017-10-10 trunk 60153) [x86_64-linux]
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>