Question

pry would be great for debugging a subclass of BasicObject !

https://github.com/pry/pry says that pry has: "Exotic object support (BasicObject instances..."

But how to do that? As can be expected a BasicObject does not understand binding.

 NameError:
   undefined local variable or method `binding' for #<C30Course:0xbefbc0c>

When method_missing is invoked, where to send binding?

Was it helpful?

Solution

You'll need to directly call the binding method on Kernel like this:

[13] pry(main)> class O < BasicObject
              |   def hi
              |     x = 10
              |     ::Kernel.binding.pry
              |   end  
              | end  
=> nil
[14] pry(main)> O.new.hi

From: (pry) @ line 19 O#hi:

    17: def hi
    18:   x = 10
 => 19:   ::Kernel.binding.pry
    20: end

[1] pry(unknown)> x
=> 10
[2] pry(unknown)> self
=> #<O:0x3fd5310d04f8>

OTHER TIPS

Since inheriting from BasicObject will give you a Blank Slate (all the pry instance method removed too), you'll have to do it a bit more manual. For instance:

require 'pry'
class Test < BasicObject
  def test_method
    a = 1+1
    ::Pry.send(:binding).pry
    b = 2+2
  end
end
o = Test.new
p o.test_method

output (pry session opened):

☺  ruby hack.rb                                                            ruby-2.0.0-p0

From: .../quick_hacking/hack.rb @ line 5 Test#test_method:

    3: def test_method
    4:   a = 1+1
 => 5:   ::Pry.send(:binding).pry
    6:   b = 2+2
    7: end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top