Skip to content

Commit 5843616

Browse files
authored
Make show_source resolve top-level constant names (#831)
1 parent 08208ad commit 5843616

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/irb/source_finder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(irb_context)
1919
def find_source(signature, super_level = 0)
2020
context_binding = @irb_context.workspace.binding
2121
case signature
22-
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
22+
when /\A(::)?[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
2323
eval(signature, context_binding) # trigger autoload
2424
base = context_binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object }
2525
file, line = base.const_source_location(signature)

test/irb/cmd/test_show_source.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,33 @@ def foo
272272

273273
assert_match(%r[#{@ruby_file.to_path}:2\s+def foo\r\n end], out)
274274
end
275+
276+
def test_show_source_with_double_colons
277+
write_ruby <<~RUBY
278+
class Foo
279+
end
280+
281+
class Foo
282+
class Bar
283+
end
284+
end
285+
286+
binding.irb
287+
RUBY
288+
289+
out = run_ruby_file do
290+
type "show_source ::Foo"
291+
type "exit"
292+
end
293+
294+
assert_match(%r[#{@ruby_file.to_path}:1\s+class Foo\r\nend], out)
295+
296+
out = run_ruby_file do
297+
type "show_source ::Foo::Bar"
298+
type "exit"
299+
end
300+
301+
assert_match(%r[#{@ruby_file.to_path}:5\s+class Bar\r\n end], out)
302+
end
275303
end
276304
end

0 commit comments

Comments
 (0)