Project

General

Profile

Actions

Bug #22331

open

Ruby::Box: a box's top self lacks the top-level definition methods

Bug #22331: Ruby::Box: a box's top self lacks the top-level definition methods

Added by hsbt (Hiroshi SHIBATA) 3 days ago.

Status:
Open
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-09-17T00:01:17Z master 3f6143715a) [arm64-darwin27]
[ruby-core:126763]

Description

Under RUBY_BOX=1, loading a file into a box fails on private, public, include, using, define_method and ruby2_keywords.

# toplevel.rb
private def hidden; end
puts "loaded"
$ RUBY_BOX=1 ruby -W:no-experimental -e 'Ruby::Box.new.load(File.expand_path("toplevel.rb"))'
toplevel.rb:1:in '<top (required)>': undefined method 'private' for #<Object:0x000000012421c180> (NoMethodError)

$ RUBY_BOX=1 ruby -W:no-experimental -e 'load(File.expand_path("toplevel.rb"), true)'
toplevel.rb:1:in '<top (required)>': undefined method 'private' for #<Object:0x00000001204bc180> (NoMethodError)

Without a box the same wrapped load only warns. Asking the loaded file what it has shows the difference:

$ ruby -e 'load(File.expand_path("show.rb"), true)'
[:define_method, :include, :private, :public, :ruby2_keywords, :using]

$ RUBY_BOX=1 ruby -W:no-experimental -e 'Ruby::Box.new.load(File.expand_path("show.rb"))'
[]

The six methods are defined during setup on the singleton class of rb_vm_top_self() (eval.c:2311, vm_method.c:3737, proc.c:5477), before the root box exists, so all six land on the master box's top self. box_entry_initialize() (box.c:170) then gives every other box a fresh object carrying only to_s and inspect, and load_wrapping() (load.c:848) clones the current box's one. Top level still sees the master box's object, so the gap shows only from a file loaded into a box.

So the question is what a box's top self should be. Three shapes I can see:

They differ in whether a singleton method added to main in one box is visible in another. Is there another shape you would prefer?

The same identity gap also breaks four autoload specs under a box.

Reproduced on master 3f6143715a and on released 4.0.7.

Related to #22275


Related issues 1 (1 open0 closed)

Related to Ruby - Misc #22275: Ruby::Box support plan for RubyGems and BundlerOpenActions

Updated by hsbt (Hiroshi SHIBATA) 3 days ago Actions #1

  • Related to Misc #22275: Ruby::Box support plan for RubyGems and Bundler added
Actions

Also available in: PDF Atom