Skip to content

Load a .rb file only once in irb_require - #1257

Open
youdie006 wants to merge 1 commit into
ruby:masterfrom
youdie006:irb-require-rb-once
Open

youdie006 wants to merge 1 commit into
ruby:masterfrom
youdie006:irb-require-rb-once

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

For a file name ending in .rb, irb_require does if irb_load(file_name) (lib/irb/command/load.rb:54). irb_load returns the value of eval_input, which is nil, so the file is evaluated but never pushed to $", and execution falls through to irb_load(file_name + ".rb") (LoadError) and then Kernel#require, which evaluates it again.

With a file that does $count += 1 on the load path:

irb> $count = 0
irb> irb_require 'probe.rb'
irb> $count   # master: 2, this PR: 1

This calls irb_load without the check, like the no-extension branch below it. A file that is not on the load path still raises LoadError inside the begin and falls back to Kernel#require as before.

The new test sits between the other irb_require tests in test/irb/test_command.rb and fails on master. test/irb/test_command.rb passes on Ruby 3.4 (57 tests), the irb_require/irb_load tests on 2.7, and rubocop reports no offenses on the changed files; I did not run the full rake test matrix.

Written with AI assistance (Claude); I have reviewed the change.

irb_load returns the value of eval_input, which is nil, so the
'if irb_load(file_name)' branch never recorded the file and fell through
to Kernel#require, which loaded it a second time. Call irb_load without
the check, as the no-extension branch does.

This branch has not been deployed

No deployments
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.

1 participant