sinatra_debugger_example icon indicating copy to clipboard operation
sinatra_debugger_example copied to clipboard

Best Practice: How to debug sinatra with debugger and pry

Objective

Simply Disply How to debug sinatra application with debugger or pry

especially, I would like to step in method debug like below

step

How to Use

# added to the Gemfile
group :development, :test do
  gem 'awesome_print'
  gem 'racksh'
  gem 'debugger'
  gem 'pry'
  gem 'pry-debugger'
end

You can just use debugger, if you run it ruby my_app.rb

class MyApp < Sinatra::Base
  enable :sessions
  get '/' do
    debugger
    session[:user] = 'anonymouse'
    redirect to('/bar')
  end

visit at http://localhost:4567

You can use pry, if you run it rackup

class MyApp < Sinatra::Base
  enable :sessions
  get '/' do
    binding.pry
    session[:user] = 'anonymouse'
    redirect to('/bar')
  end

in case to use the short cut of next contine step finish add pry config

#.pryrc
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'

visit at http://localhost:9292

looks like

ruby my_app.rb

debugger

rackup

pry

sinatra console

You may also would like to know how to run console for sinatra and simulate a get or post call from console, try racksh

racksh