Skip to content

Instantly share code, notes, and snippets.

@dskecse
Created December 26, 2025 13:18
Show Gist options
  • Select an option

  • Save dskecse/75e3c38b0fb4bf1b6c7e3d8296402272 to your computer and use it in GitHub Desktop.

Select an option

Save dskecse/75e3c38b0fb4bf1b6c7e3d8296402272 to your computer and use it in GitHub Desktop.
Ruby 4.0 Box
# frozen_string_literal: true
X = 1
class Rubybox
def self.x = X
def x = ::X
end
# Usage:
#
# irb
# > box = Ruby::Box.new
# (irb):1:in 'Ruby::Box#initialize': Ruby Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box. (RuntimeError)
# > Ruby::Box.enabled?
# => false
#
# RUBY_BOX=1 irb
# /Users/dskecse/.rubies/ruby-4.0.0/bin/ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
# See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc.
# > box = Ruby::Box.new
# => #<Ruby::Box:3,user,optional>
# > box.require_relative("rubybox")
# => true
# > X = 2
# => 2
# > p X
# 2
# => 2
# > p ::X
# 2
# => 2
# > p box::Rubybox.x
# 1
# => 1
# > p box::Rubybox.new.x
# 1
# => 1
# > p box::X
# 1
# => 1
#
# > Ruby::Box.main
# => #<Ruby::Box:2,user,main>
# > Ruby::Box.root
# => #<Ruby::Box:1,root>
# > Ruby::Box.current
# => #<Ruby::Box:1,root>
@dskecse
Copy link
Author

dskecse commented Dec 26, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment