From 5d6e1ce7b2a29a75a3cd278ed2f9f75880f6e9e1 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 14 Oct 2011 00:02:55 -0700 Subject: [PATCH] Add Pry::Method#super --- lib/pry/method.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index d9368f74..073da94d 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -265,6 +265,18 @@ class Pry "#{name}(#{args.join(', ')})" end + # @return [Pry::Method, nil] The wrapped method that get's called when you + # use "super" in the body of this method. + def super(times=1) + if respond_to?(:receiver) + sup = super_using_ancestors(Pry::Method.resolution_order(receiver), times) + sup &&= sup.bind(receiver) + else + sup = super_using_ancestors(Pry::Method.instance_resolution_order(owner), times) + end + Pry::Method.new(sup) if sup + end + # @return [Boolean] Was the method defined outside a source file? def dynamically_defined? !!(source_file and source_file =~ /(\(.*\))|<.*>/) @@ -333,5 +345,16 @@ class Pry def strip_leading_whitespace(text) Pry::Helpers::CommandHelpers.unindent(text) end + + # @param [Class,Module] the ancestors to investigate + # @return [Method] the unwrapped super-method + def super_using_ancestors(ancestors, times=1) + next_owner = self.owner + times.times do + next_owner = ancestors[ancestors.index(next_owner) + 1] + return nil unless next_owner + end + next_owner.instance_method(name) rescue nil + end end end