From 5843616c78ba9d2798d58f05543a5fe8c749d093 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 1 Jan 2024 17:40:35 +0000 Subject: [PATCH] Make show_source resolve top-level constant names (#831) --- lib/irb/source_finder.rb | 2 +- test/irb/cmd/test_show_source.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb index 659d4200f..4e9cdc462 100644 --- a/lib/irb/source_finder.rb +++ b/lib/irb/source_finder.rb @@ -19,7 +19,7 @@ def initialize(irb_context) def find_source(signature, super_level = 0) context_binding = @irb_context.workspace.binding case signature - when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name + when /\A(::)?[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name eval(signature, context_binding) # trigger autoload base = context_binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object } file, line = base.const_source_location(signature) diff --git a/test/irb/cmd/test_show_source.rb b/test/irb/cmd/test_show_source.rb index cedec8aa6..12abae3c7 100644 --- a/test/irb/cmd/test_show_source.rb +++ b/test/irb/cmd/test_show_source.rb @@ -272,5 +272,33 @@ def foo assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out) end + + def test_show_source_with_double_colons + write_ruby <<~RUBY + class Foo + end + + class Foo + class Bar + end + end + + binding.irb + RUBY + + out = run_ruby_file do + type "show_source ::Foo" + type "exit" + end + + assert_match(%r[#{@ruby_file.to_path}:1\s+class Foo\r\nend], out) + + out = run_ruby_file do + type "show_source ::Foo::Bar" + type "exit" + end + + assert_match(%r[#{@ruby_file.to_path}:5\s+class Bar\r\n end], out) + end end end