Skip to content

Conversation

@tompng
Copy link
Member

@tompng tompng commented Nov 22, 2025

A playground to try monkey patching core methods.

% RUBY_BOX=1 irb --context-mode 5          
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See doc/box.md for known issues, etc.
Context-mode-5 (binding in new Ruby::Box) is experimental. It may be removed or changed without notice.
irb(main):001> defined? IRB
=> nil
irb(main):002* class Integer
irb(main):003*   def +(*) = 42
irb(main):004> end
=> :+
irb(main):005> 1 + 2
=> 42
irb(main):006> 

Related to: #143 #235 #960

@binding = TOPLEVEL_BINDING.dup
when 5
puts 'Context-mode-5 (binding in new Ruby::Box) is experimental. It may be removed or changed without notice.'
@binding = Ruby::Box.new.eval('Kernel.binding')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we include a check for the Ruby version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be a constant defined check instead? If Ruby::Box doesn't exist, then we should warn that it's not available and that we'd fallback to the default behaviour.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a defined?(Ruby::Box) check.
I think if Ruby::Box is unavailable, it's OK or even safe to fail instead of fallback to default.

There is one more error case. If Box is not enabled by ENV['RUBY_BOX'], class Ruby::Box is defined but calling new throws RuntimeError.

'Ruby::Box#initialize': Ruby Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box. (RuntimeError)

@st0012
Copy link
Member

st0012 commented Nov 22, 2025

Can we have a few simple test cases running with this mode on? It can be used to detect Ruby::Box's regression and we can also proactively disable it if it becomes noisy.

when 5 # binding in Ruby::Box
unless defined?(Ruby::Box)
puts 'Context-mode 5 (binding in Ruby::Box) requires Ruby 4.0 or later.'
raise NameError, 'Ruby::Box not defined'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediate exit will make IRB.start rescue do_something not work as expected

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants