How to use Pry with Sinatra?
Summary
- Use
require 'pry'
at the top of your application. - Call
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. - When you are done with a particular interactive session, type
exit
or Ctrl-D; Sinatra will resume running where it left off.
Example
require 'sinatra'
require 'pry'
get '/' do
@cats = rand(100)
html = haml :index
binding.pry
html
end
__END__
@@index
%html
<head><title>Hello World</title></head>
%body
%p I have #{@cats} cat#{:s unless @cats==1}!
Here's what it looks like when I start the web server:
C:\>ruby pry_into_sinatra.rb
== Sinatra/1.2.6 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
When I make a request in a web browser to http://localhost:4567 the console drops into the Pry debugger before sending the result:
From: pry_into_sinatra.rb @ line 7 in Sinatra::Application#HEAD /:
2: require 'pry'
3:
4: get '/' do
5: @cats = rand(100)
6: html = haml :index
=> 7: binding.pry
8: html
9: end
10:
11: __END__
12: @@index
pry(#<Sinatra::Application:0x3300ac8>)> @cats
=> 42
pry(#<Sinatra::Application:0x3300ac8>)> puts html
<html>
<head><title>Hello World</title></head>
<body>
<p>I have 42 cats!</p>
</body>
</html>
=> nil
pry(#<Sinatra::Application:0x3300ac8>)> exit
127.0.0.1 - - [24/Aug/2011 13:25:57] "GET / HTTP/1.1" 200 96 28.5390
127.0.0.1 - - [24/Aug/2011 13:25:57] "GET /favicon.ico HTTP/1.1" 404 447 0.0010
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.
Using pry with Sinatra
It all depends on where you want to use pry. If you want it to work everywhere in your Sinatra project, then just add it to your Gemfile.
group :development, :test do
gem 'pry'
end
I would recommend limiting the environments to :development and :test, since you will be doing your debugging there.
You can also require it in the top of your application like so:
require 'pry'
Debugging Sinatra application
In your Gemfile:
gem "pry"
gem "pry-nav"
Run bundle. Then whenever the script encounters binding.pry
, you will have a shell opened. You can see where you are with whereami
, you move to the next line with next
, continue running the script with continue
. If you want to inspect a variable, just type its name.
See pry-nav for more info.
How to use pry to access a Rack middleware instance?
Not sure what you are trying to do, but you can
Monkey patch the middleware class to use pry inside
connect directly from the source code (what I understood you dont want)
play around with the "use" method from Sinatra to access the middleware you want from the middleware stack
That are my ideas, hope it helps
AFTER YOUR EDITED:
Check out this, you can access the @middleware instance variable to get the used middlewares
How do you debug a Sinatra app like a Rails app?
You could try adding a before filter that prints out the parameters
before do
puts '[Params]'
p params
end
Related Topics
Mongodb with Mongoid in Rails - Geospatial Indexing
Ruby - Can't Modify Frozen String (Typeerror)
"Uninitialized Constant" Error When Including a Module
Generate and Publish Ruby Based Rest APIs Documentation
Rails-Like Database Migrations
Is 'Yield Self' the Same as Instance_Eval
Why Does Ruby Open-Uri's Open Return a Stringio in My Unit Test, But a Fileio in My Controller
How to Password-Protect My /Sidekiq Route (I.E. Require Authentication for the Sidekiq::Web Tool)
Stream Console Output Through Http (With Ruby)
Generate All Possibles Combinations of an Array with a Length Within a Given Range
Rails Email Error - 530-5.5.1 Authentication Required
Tzinfo-Data Present But Not Seen
Ruby Array to String Conversion
Upgrading from Rails 3 to Rails 3.1
How to Use Ruby Dbi's 'Select_All' VS 'Execute-Fetch/Each-Finish'
Optimization for Finding Perfect-Square Algorithm