I am writing my first Sinatra application and would like to use Pry to inspect/debug some things going on in the application. I haven't used Pry before either, but I would like to try it out. How would I get started using Pry with my Sinatra application?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
Load the application into a Pry session:
Take a look at your
config.ru
. If it looks something like this:you can load your application into Pry using
This can be done with any module or class so long as dependencies are met.
Look at this Pry cheat sheet for advanced examples of Pry usage.
Summary
require 'pry'
at the top of your application.binding.pry
in your code whenever you want to drop into the interactive session. For information on using Pry, see Turning IRB on its head with Pry and the Pry wiki.exit
or Ctrl-D; Sinatra will resume running where it left off.Example
Here's what it looks like when I start the web server:
When I make a request in a web browser to http://localhost:4567 the console drops into the Pry debugger before sending the result:
Further Debugging
If you want to be able to use traditional debugging commands, such as setting line-based breakpoints, or stepping, or breaking when exceptions are raised, see the PryDebug library by Mon-Ouie.
My preferred method is also Pry, but a little bit different to above. In one of the first files to run in a process, say the
config.ru
or thespec/spec_helper.rb
:Then if I want to using debugging, I run
env DEBUG=1 bin/rackup config.ru
orenv DEBUG=1 bin/rspec
(I use it a lot with the-e
switch in RSpec) and then set break points usingbreak
. It means I don't have to change the code at all to drop into it.I prefer pry-debugger. However there is still trick, that you can not pry-stepping while you run sinatra under classic style.
In order to find the best way to debug sinatra app, I created a repo at github, which looks like below.
Here is the repo: https://github.com/hlee/sinatra_debugger_example