Skip to content

Add --context-mode 5: evaluates inside Ruby::Box#1142

Merged
tompng merged 2 commits into
ruby:masterfrom
tompng:box_context_mode
Dec 4, 2025
Merged

Add --context-mode 5: evaluates inside Ruby::Box#1142
tompng merged 2 commits into
ruby:masterfrom
tompng:box_context_mode

Conversation

@tompng

@tompng tompng commented Nov 22, 2025

Copy link
Copy Markdown
Member

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

Comment thread lib/irb/workspace.rb
@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

st0012 commented Nov 22, 2025

Copy link
Copy Markdown
Member

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.

Comment thread lib/irb/workspace.rb
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
Copy Markdown
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

Comment thread test/irb/test_irb.rb

class ContextModeTest < TestIRB::IntegrationTestCase
# RUBY_BOX=1 may need more time to start IRB
TIMEOUT_SEC = TestIRB::IntegrationTestCase::TIMEOUT_SEC + 5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this can be

Suggested change
TIMEOUT_SEC = TestIRB::IntegrationTestCase::TIMEOUT_SEC + 5
TIMEOUT_SEC = self::TIMEOUT_SEC + 5
class Foo
  TIMEOUT = 10
end

class Bar < Foo
  TIMEOUT = self::TIMEOUT + 10
end

puts Bar::TIMEOUT # 20

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, it can be even shorter

TIMEOUT = TIMEOUT + 5

or

TIMEOUT += 5

but the visual of the code, especially the last one, looks like a constant reassignment. (It's not, though)
So my code is... a bit redundant but I'd like to use the redundant form here.

@st0012 st0012 added the enhancement New feature or request label Dec 4, 2025
@tompng tompng merged commit 847d1ca into ruby:master Dec 4, 2025
33 checks passed
@tompng tompng deleted the box_context_mode branch December 5, 2025 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

3 participants