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?
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With