Skip to content

Commit

Permalink
This enhancement allows a user to add the -s flag if they want to acc…
Browse files Browse the repository at this point in the history
…ess a methods origin definition. It allows for chaining of multiple esses to further go up the classes as needed.
  • Loading branch information
paulreece committed Nov 19, 2023
1 parent afbba64 commit 6e361f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def bold(str)
end
end
end
end
end
8 changes: 8 additions & 0 deletions lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def find_source(signature)
when /\A(?<owner>[A-Z]\w*(::[A-Z]\w*)*)#(?<method>[^ :.]+)\z/ # Class#method
owner = eval(Regexp.last_match[:owner], context_binding)
method = Regexp.last_match[:method]
if signature.include? " -s"
signature, esses = signature.split(" -")
super_owner = owner
esses.split("").each {|s| super_owner = super_owner.superclass}
if super_owner.respond_to?(:instance_method)
owner = super_owner
end
end
if owner.respond_to?(:instance_method)
methods = owner.instance_methods + owner.private_instance_methods
file, line = owner.instance_method(method).source_location if methods.include?(method.to_sym)
Expand Down
11 changes: 11 additions & 0 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ def test_show_source_method
assert_match(%r[/irb\/init\.rb], out)
end

def test_show_source_method_s
File.write("#{@tmpdir}/bazbar.rb", "class Baz\n def foo\n end\nend\n\nclass Bar < Baz\n def foo\n super\n end\nend\n")
out, err = execute_lines(
"irb_load '#{@tmpdir}/bazbar.rb'\n",
"show_source Bar#foo -s",
)
assert_empty err
p out
assert_match(%r[ => :foo\n => :foo\n], out)
end

def test_show_source_string
out, err = execute_lines(
"show_source 'IRB.conf'\n",
Expand Down

0 comments on commit 6e361f1

Please sign in to comment.