Project

General

Profile

Actions

Bug #21799

open

Delegated top-level methods are not visible outside the Ruby Box

Bug #21799: Delegated top-level methods are not visible outside the Ruby Box

Added by koic (Koichi ITO) about 23 hours ago. Updated about 23 hours ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.0dev (2025-12-21T19:08:00Z master cfb324e9d1) +PRISM [arm64-darwin24]
[ruby-core:124340]

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.

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

Actions

Also available in: PDF Atom