Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop in Single Breakpoint in Ruby Code

I am trying to find ruby code that has commensurate functionality to these lines in python:

import code
code.interact(local=locals())

These lines essentially insert a single breakpoint into my code and open up a console where I can interact with any variables.

Any thoughts on how to do this in Ruby?

like image 714
Spencer Avatar asked Dec 01 '11 20:12

Spencer


People also ask

How do I debug a Ruby code?

To help deal with bugs, the standard distribution of Ruby includes a debugger. In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.

How do you use Byebug in Ruby?

wherever you'd like the application to "break" - that is, executing byebug is equivalent to putting a breakpoint in your code. Run the program and use the debugger commands once you reach the breakpoint. near the end. Restart your server.


2 Answers

You want the Pry library:

require 'pry' # gem install pry
binding.pry   # Drop into the pry console

Read more here:
http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/

See also:
How to use Pry with Sinatra?

like image 150
Phrogz Avatar answered Sep 28 '22 10:09

Phrogz


There is Kernel#local_variables in Ruby that returns the names of the current local variables. Check out the docs:

ri local_variables
like image 36
Marek Příhoda Avatar answered Sep 28 '22 09:09

Marek Příhoda