From: "shioyama (Chris Salzberg)" Date: 2022-10-18T12:05:28+00:00 Subject: [ruby-core:110404] [Ruby master Bug#19067] Module methods not usable at toplevel under wrapped script Issue #19067 has been updated by shioyama (Chris Salzberg). This is actually even confusing prior to the recent change: > If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program's global namespace. ```ruby # foo.rb def foo end foo ``` ```ruby load "foo.rb", true ``` This "works", but it does not do this, which is what I think would be expected: ```ruby Module.new do def foo end foo end # undefined local variable or method `foo' for # (NameError) ``` I think what `load` actually does is powerful and I've stated so elsewhere, but it is not simply "executing it under an anonymous namespace". ---------------------------------------- Bug #19067: Module methods not usable at toplevel under wrapped script https://bugs.ruby-lang.org/issues/19067#change-99714 * Author: shioyama (Chris Salzberg) * Status: Open * Priority: Normal * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- ```ruby #foo.rb module Foo end private_constant :Foo ``` ```ruby module MyModule; end load "./foo.rb", MyModule # undefined method `private_constant' for main:Object (NoMethodError) # # private_constant :Foo # ^^^^^^^^^^^^^^^^ # Did you mean? private_methods ``` However, this works: ```ruby module MyModule module Foo end private_constant :Foo end ``` `load` loads the code under the `wrap` module, so this seems like a bug to me. This applies to all methods on `Module`, which are usable inside a normal module definition but not usable when the module is the `wrap` argument to `load`. I think these should all be usable in this context. If they cannot, then clarity is needed about what "being executed under" means in the documentation for `load`. See: https://bugs.ruby-lang.org/issues/19024#note-23 -- https://bugs.ruby-lang.org/ Unsubscribe: