Permalink
Browse files

Use ::Kernel.__method__ so that we can work on BasicObject

  • Loading branch information...
1 parent baaa85a commit f62ced6f385ab8bea14dc461d38df251bcc8c0ce @ConradIrwin ConradIrwin committed Sep 24, 2012
Showing with 31 additions and 3 deletions.
  1. +1 −1 lib/pry/helpers/command_helpers.rb
  2. +6 −2 lib/pry/method.rb
  3. +12 −0 test/test_commands/test_whereami.rb
  4. +12 −0 test/test_method.rb
@@ -40,7 +40,7 @@ def file_and_line_from_binding(target)
end
def internal_binding?(target)
- m = target.eval("__method__").to_s
+ m = target.eval("::Kernel.__method__").to_s
# class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
["__binding__", "__pry__", "class_eval"].include?(m)
end
View
@@ -62,12 +62,16 @@ def from_str(name, target=TOPLEVEL_BINDING, options={})
# @return [Pry::Method, nil]
#
def from_binding(b)
- meth_name = b.eval('__method__')
+ meth_name = b.eval('::Kernel.__method__')
if [:__script__, nil].include?(meth_name)
nil
else
method = begin
- new(Object.instance_method(:method).bind(b.eval("self")).call(meth_name))
+ if Object === b.eval('self')
+ new(Kernel.instance_method(:method).bind(b.eval("self")).call(meth_name))
+ else
+ new(b.eval('class << self; self; end.instance_method(::Kernel.__method__).bind(self)'))
+ end
rescue NameError, NoMethodError
Disowned.new(b.eval('self'), meth_name.to_s)
end
@@ -41,6 +41,18 @@ def blimey!
Object.remove_const(:Cor)
end
+ if defined?(BasicObject)
+ it 'should work in BasicObjects' do
+ cor = Class.new(BasicObject) do
+ def blimey!
+ ::Kernel.binding # omnom
+ end
+ end.new.blimey!
+
+ pry_eval(cor, 'whereami').should =~ /::Kernel.binding [#] omnom/
+ end
+ end
+
it 'should show description and correct code when __LINE__ and __FILE__ are outside @method.source_location' do
class Cor
def blimey!
View
@@ -119,6 +119,18 @@ class << self; undef bar; end
m.source_line.should == b.line
m.name.should == "gag"
end
+
+ if defined? BasicObject
+ it "should find the right method from a BasicObject" do
+ a = Class.new(BasicObject) { def gag; ::Kernel.binding; end; def self.line; __LINE__; end }
+
+ m = Pry::Method.from_binding(a.new.gag)
+
+ m.owner.should == a
+ m.source_file.should == __FILE__
+ m.source_line.should == a.line
+ end
+ end
end
describe 'super' do

0 comments on commit f62ced6

Please sign in to comment.