Last Updated: January 23, 2019
·
8.476K
· locks

Debug your Sinatra application easily

Using a gem named pry you can easily inspect the state of your web application while it's running with the pry-remote plugin.

Let's see how.

Setup

Let's start by adding pry to your Gemfile.
(Don't forget to bundle install afterwards)

# Gemfile
gem "pry"
gem "pry-remote"

Then in your config.ru you just need to require pry-remote.

# config.ru
require 'pry-remote'

Binding

Next let's go to our Sinatra app, and put binding.remote_pry wherever it is you want to start your pry session.

# app.rb
get '/' do
  # some really complicated and convoluted code that makes your head hurt

  binding.remote_pry

end

Remotinizing

Now just hit the / route on your application.

You will notice that the browser will hang without rendering the page, that's because a pry-remote session was started.

To access it go to your command line and type:

$ pry-remote

From: /Users/locks/repo/api.rb @ line 15 self.GET /:

    10:     
    11:     get '/' do
    12:       # some really complicated and convoluted code that makes your head hurt
    14:       
 => 15:       binding.remote_pry
    17:
    16:     end

And now you're ready to have fun!

Useful links

Interesting plugins

  • pry-debugger - full-blown debugger inside your pry session.
  • pry-rescue - starts a pry session whenever an exception is thrown.