Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding.pry in BasicObject

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?

like image 946
Ernst Avatar asked Apr 14 '13 21:04

Ernst


People also ask

How do I run binding pry?

The Ruby programmer can invoke the pry console during runtime by inserting the line 'binding. pry' wherever they would like to stop the program. When the interpreter hits the binding. pry, Pry will open a REPL session in the console, allowing you to test variables, return values, iterations, and more.

How do I exit all binding pry?

If you are using Pry in your project, you might run into situation where you are in a loop or having multiple binding. pry and so you have to use exit multiple times to get out from it. So, instead of using exit multiple times, you can use exit-program to continue the execution without breaking into Pry again.

What is GEM pry?

Pry is a runtime developer console and IRB alternative with powerful introspection capabilities. Pry aims to be more than an IRB replacement. It is an attempt to bring REPL driven programming to the Ruby language.

What is binding in Ruby?

To keep track of the current scope, Ruby uses bindings, which encapsulate the execution context at each position in the code. The binding method returns a Binding object which describes the bindings at the current position. foo = 1 binding.


1 Answers

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>
like image 101
horseyguy Avatar answered Sep 20 '22 05:09

horseyguy