Bug #21799
Updated by byroot (Jean Boussier) 1 day ago
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
```ruby
# main.rb
box = Ruby::Box.new
box.require_relative('foo')
p box.Foo.say #=> "foo"
p yay # NoMethodError
```
```ruby
# foo.rb main.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`.
```console
$ 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.
https://github.com/ruby/ruby/blob/v4.0.0-preview3/doc/language/box.md#expose-top-level-methods-as-a-method-of-the-box-object