Created
December 26, 2025 13:18
-
-
Save dskecse/75e3c38b0fb4bf1b6c7e3d8296402272 to your computer and use it in GitHub Desktop.
Ruby 4.0 Box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.ruby-lang.org/en/master/Ruby/Box.html