summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/cmd/show_source.rb13
-rw-r--r--lib/irb/source_finder.rb18
2 files changed, 11 insertions, 20 deletions
diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb
index 9a0364e3eb..826cb11ed2 100644
--- a/lib/irb/cmd/show_source.rb
+++ b/lib/irb/cmd/show_source.rb
@@ -27,17 +27,14 @@ module IRB
puts "Error: Expected a string but got #{str.inspect}"
return
end
- if str.include? " -s"
- str, esses = str.split(" -")
- s_count = esses.count("^s").zero? ? esses.size : 1
- source = SourceFinder.new(@irb_context).find_source(str, s_count)
- else
- source = SourceFinder.new(@irb_context).find_source(str)
- end
+
+ str, esses = str.split(" -")
+ super_level = esses ? esses.count("s") : 0
+ source = SourceFinder.new(@irb_context).find_source(str, super_level)
if source
show_source(source)
- elsif s_count
+ elsif super_level > 0
puts "Error: Couldn't locate a super definition for #{str}"
else
puts "Error: Couldn't locate a definition for #{str}"
diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb
index a0aedcee68..659d4200fd 100644
--- a/lib/irb/source_finder.rb
+++ b/lib/irb/source_finder.rb
@@ -16,7 +16,7 @@ module IRB
@irb_context = irb_context
end
- def find_source(signature, s_count = nil)
+ 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
@@ -27,12 +27,12 @@ module IRB
owner = eval(Regexp.last_match[:owner], context_binding)
method = Regexp.last_match[:method]
return unless owner.respond_to?(:instance_method)
- file, line = method_target(owner, s_count, method, "owner")
+ file, line = method_target(owner, super_level, method, "owner")
when /\A((?<receiver>.+)(\.|::))?(?<method>[^ :.]+)\z/ # method, receiver.method, receiver::method
receiver = eval(Regexp.last_match[:receiver] || 'self', context_binding)
method = Regexp.last_match[:method]
return unless receiver.respond_to?(method, true)
- file, line = method_target(receiver, s_count, method, "receiver")
+ file, line = method_target(receiver, super_level, method, "receiver")
end
if file && line && File.exist?(file)
Source.new(file: file, first_line: line, last_line: find_end(file, line))
@@ -60,20 +60,14 @@ module IRB
first_line
end
- def method_target(owner_receiver, s_count, method, type)
+ def method_target(owner_receiver, super_level, method, type)
case type
when "owner"
target_method = owner_receiver.instance_method(method)
- return target_method.source_location unless s_count
when "receiver"
- if s_count
- target_method = owner_receiver.class.instance_method(method)
- else
- target_method = method
- return owner_receiver.method(method).source_location
- end
+ target_method = owner_receiver.method(method)
end
- s_count.times do |s|
+ super_level.times do |s|
target_method = target_method.super_method if target_method
end
target_method.nil? ? nil : target_method.source_location