From a9db4054dbb6f6f5ff7b364af556d4a5e06f7e0d Mon Sep 17 00:00:00 2001 From: Kirill Lashuk Date: Wed, 30 Nov 2011 01:54:55 +0300 Subject: [PATCH] Fetch method name for 'whereami' directly from binding. Pry::Method.from_str fails to find by name unbound method that was removed from the class. As 'whereami' command needs only name of the method it can fetch that name directly from binding without performing lookup of the method itself. --- lib/pry/default_commands/context.rb | 6 +----- lib/pry/method.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/pry/default_commands/context.rb b/lib/pry/default_commands/context.rb index 4d4b153a..e97eef22 100644 --- a/lib/pry/default_commands/context.rb +++ b/lib/pry/default_commands/context.rb @@ -143,11 +143,7 @@ class Pry i_num = 5 end - if (meth = Pry::Method.from_binding(target)) - meth_name = meth.name - else - meth_name = "N/A" - end + meth_name = Pry::Method.method_name_from_binding(target) || "N/A" if file != Pry.eval_path && (file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e") raise CommandError, "Cannot find local context. Did you use `binding.pry`?" diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 16c05e55..446111e5 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -45,7 +45,7 @@ class Pry # @return [Pry::Method, nil] # def from_binding(b) - meth_name = b.eval('__method__') + meth_name = method_name_from_binding(b) if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name) nil else @@ -113,6 +113,14 @@ class Pry ([klass] + klass.ancestors).uniq end + # Given a `Binding` extract name of the method it originated from. + # Return `nil` if no such method exists. + # @param [Binding] b + # @return [Symbol, nil] + def method_name_from_binding(b) + b.eval('__method__') + end + private # See all_from_class and all_from_obj.