Skip to content

Commit 2cccc44

Browse files
authored
Simplify show_source's super calculation (#807)
1 parent b7b5731 commit 2cccc44

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

lib/irb/cmd/show_source.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@ def execute(str = nil)
2727
puts "Error: Expected a string but got #{str.inspect}"
2828
return
2929
end
30-
if str.include? " -s"
31-
str, esses = str.split(" -")
32-
s_count = esses.count("^s").zero? ? esses.size : 1
33-
source = SourceFinder.new(@irb_context).find_source(str, s_count)
34-
else
35-
source = SourceFinder.new(@irb_context).find_source(str)
36-
end
30+
31+
str, esses = str.split(" -")
32+
super_level = esses ? esses.count("s") : 0
33+
source = SourceFinder.new(@irb_context).find_source(str, super_level)
3734

3835
if source
3936
show_source(source)
40-
elsif s_count
37+
elsif super_level > 0
4138
puts "Error: Couldn't locate a super definition for #{str}"
4239
else
4340
puts "Error: Couldn't locate a definition for #{str}"

lib/irb/source_finder.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(irb_context)
1616
@irb_context = irb_context
1717
end
1818

19-
def find_source(signature, s_count = nil)
19+
def find_source(signature, super_level = 0)
2020
context_binding = @irb_context.workspace.binding
2121
case signature
2222
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
@@ -27,12 +27,12 @@ def find_source(signature, s_count = nil)
2727
owner = eval(Regexp.last_match[:owner], context_binding)
2828
method = Regexp.last_match[:method]
2929
return unless owner.respond_to?(:instance_method)
30-
file, line = method_target(owner, s_count, method, "owner")
30+
file, line = method_target(owner, super_level, method, "owner")
3131
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
3232
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
3333
method = Regexp.last_match[:method]
3434
return unless receiver.respond_to?(method, true)
35-
file, line = method_target(receiver, s_count, method, "receiver")
35+
file, line = method_target(receiver, super_level, method, "receiver")
3636
end
3737
if file && line && File.exist?(file)
3838
Source.new(file: file, first_line: line, last_line: find_end(file, line))
@@ -60,20 +60,14 @@ def find_end(file, first_line)
6060
first_line
6161
end
6262

63-
def method_target(owner_receiver, s_count, method, type)
63+
def method_target(owner_receiver, super_level, method, type)
6464
case type
6565
when "owner"
6666
target_method = owner_receiver.instance_method(method)
67-
return target_method.source_location unless s_count
6867
when "receiver"
69-
if s_count
70-
target_method = owner_receiver.class.instance_method(method)
71-
else
72-
target_method = method
73-
return owner_receiver.method(method).source_location
74-
end
68+
target_method = owner_receiver.method(method)
7569
end
76-
s_count.times do |s|
70+
super_level.times do |s|
7771
target_method = target_method.super_method if target_method
7872
end
7973
target_method.nil? ? nil : target_method.source_location

test/irb/cmd/test_show_source.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ def test_show_source_alias
3939
assert_match(%r[/irb\/init\.rb], out)
4040
end
4141

42+
def test_show_source_with_missing_signature
43+
write_ruby <<~'RUBY'
44+
binding.irb
45+
RUBY
46+
47+
out = run_ruby_file do
48+
type "show_source foo"
49+
type "exit"
50+
end
51+
52+
assert_match(%r[Couldn't locate a definition for foo], out)
53+
end
54+
4255
def test_show_source_string
4356
write_ruby <<~'RUBY'
4457
binding.irb

0 commit comments

Comments
 (0)