Skip to content

Commit

Permalink
Make show_source resolve top-level constant names (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Jan 1, 2024
1 parent 08208ad commit 5843616
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions test/irb/cmd/test_show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5843616

Please sign in to comment.