@Eregon (Benoit Daloze) is correct. This issue first started in Ruby 3.0. It does not occur when the irb --context-mode
is 1, 2, or 3 (Ruby 2.7 default), but it does occur when --context-mode
is 4 (Ruby 3.0 default) or 0. Between Ruby 2.7 and 3.0, the default irb --context-mode
switched from 3 to 4 to fix #9580.
It seems undesirable to me that irb
would have local variables predefined other than _
. While the f
local variable is RubyInstaller specific, the str
and version
local variables are not. So I think this should be considered a bug.
Assigning to @marcandre (Marc-Andre Lafortune), since he made the --context-mode
change in irb. One possible solution is to accept the new irb behavior, and modify the binstubs to not expose or create local variables. An easy way to do that:
proc do
# previous binstub code
end.call
The problem with that approach is it adds a stack frame. An alternative approach that doesn't add a stack frame is to switch to a much uglier approach using instance variables (which can be removed, unlike local variables):
require 'rubygems'
@version = ">= 0.a"
@str = ARGV.first
if @str
@str = @str.b[/\A_(.*)_\z/, 1]
if @str and Gem::Version.correct?(@str)
@version = @str
ARGV.shift
end
end
remove_instance_variable(:@str)
if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('irb', 'irb', @version.tap{remove_instance_variable(:@version)})
else
gem "irb", @version
load Gem.bin_path("irb", "irb", @version.tap{remove_instance_variable(:@version)})
end