diff options
| author | Stan Lo <stan001212@gmail.com> | 2024-01-01 17:40:35 +0000 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-01-01 17:40:38 +0000 |
| commit | 676748abcad62c021fc122d137f177d3c0f0f7a8 (patch) | |
| tree | 4469367d361b5204b1e17e66b8ded3241b6ce03b | |
| parent | 6934a60ab1d68ff89e0088a951a4c205f3c60332 (diff) | |
[ruby/irb] Make show_source resolve top-level constant names
(https://github.com/ruby/irb/pull/831)
https://github.com/ruby/irb/commit/5843616c78
| -rw-r--r-- | lib/irb/source_finder.rb | 2 | ||||
| -rw-r--r-- | test/irb/cmd/test_show_source.rb | 28 |
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb index 659d4200fd..4e9cdc462a 100644 --- a/lib/irb/source_finder.rb +++ b/lib/irb/source_finder.rb @@ -19,7 +19,7 @@ module IRB 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 cedec8aa6f..12abae3c72 100644 --- a/test/irb/cmd/test_show_source.rb +++ b/test/irb/cmd/test_show_source.rb @@ -272,5 +272,33 @@ module TestIRB 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 |
