Bug #21799
openDelegated top-level methods are not visible outside the Ruby Box
Description
The following example code based on Ruby Box results in an unexpected error.
https://github.com/ruby/ruby/blob/v4.0.0-preview3/doc/language/box.md#top-level-methods
I'm not familiar with Ruby Box, but it seems that either the above documentation example in doc/language/box.md or the Ruby Box implementation is incorrect.
Steps to reproduce¶
$ cat main.rb
box = Ruby::Box.new
box.require_relative('foo')
p box.Foo.say #=> "foo"
p yay # NoMethodError
$ cat foo.rb
def yay = "foo"
class Foo
def self.say = yay
end
p Foo.say #=> "foo"
p yay #=> "foo"
Expected¶
According to the documentation example, box.Foo.say in main.rb should return the string "foo".
Actual¶
Contrary to the documentation example, calling box.Foo.say in main.rb raises a NoMethodError.
$ RUBY_BOX=1 ruby main.rb
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See doc/language/box.md for known issues, etc.
"foo"
"foo"
main.rb:4:in '<main>': undefined method 'Foo' for module #<Ruby::Box:3,user,optional> (NoMethodError)
p box.Foo.say #=> "foo"
^^^^
Additional Information¶
According to the description below, the documentation example might not be appropriate, but it's unclear what visibility delegated top-level methods have.
Currently, top level methods in boxes are not accessible from outside of the box. But there might be a use case to call other box's top level methods.
Updated by jeremyevans0 (Jeremy Evans) about 23 hours ago
I think box.Foo.say should be box::Foo.say. https://github.com/ruby/ruby/pull/15690