From 49fd7d6041734e041aadfff521a5192085d2c636 Mon Sep 17 00:00:00 2001 From: Gabriel Lidenor Date: Fri, 11 Sep 2026 19:15:38 -0500 Subject: [PATCH] fix: irb_require matches similar names --- lib/irb/command/load.rb | 2 +- test/irb/test_command.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/irb/command/load.rb b/lib/irb/command/load.rb index 1cd3f279d..36c6dbbad 100644 --- a/lib/irb/command/load.rb +++ b/lib/irb/command/load.rb @@ -45,7 +45,7 @@ def execute(arg) def execute_internal(file_name = nil) raise_cmd_argument_error unless file_name - rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?") + rex = Regexp.new("\\A#{Regexp.escape(file_name)}(?:\\.o|\\.rb)?\\z") return false if $".find{|f| f =~ rex} case file_name diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index fd98a5a1b..5795a4457 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -458,6 +458,21 @@ def test_irb_load_without_argument assert_empty err assert_match(/Please specify the file name./, out) end + + def test_irb_require_file_matches_exact_name + File.write("#{@tmpdir}/foo.rb", "'foo_loaded'\n") + File.write("#{@tmpdir}/foo2.rb", "'foo2_loaded'\n") + + out, err = execute_lines( + "$LOAD_PATH.unshift '#{@tmpdir}'\n", + "irb_require 'foo'\n", + "irb_require 'foo2'\n", + ) + + assert_empty(err) + assert_match(/foo_loaded/, out) + assert_match(/foo2_loaded/, out) + end end class WorkspaceCommandTestCase < CommandTestCase