How to use Pry with Sinatra?

2019-01-31 07:20发布

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?

标签: ruby sinatra pry
4条回答
乱世女痞
2楼-- · 2019-01-31 07:40

Load the application into a Pry session:

Take a look at your config.ru. If it looks something like this:

require File.join(File.dirname(__FILE__), 'config', 'application.rb')

you can load your application into Pry using

bundle exec pry -I . -r config/application.rb
# where -I . adds current dir to load path
# and -r is the file you want to require

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.

查看更多
混吃等死
3楼-- · 2019-01-31 07:41

Summary

  1. Use require 'pry' at the top of your application.
  2. 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.
  3. 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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-31 07:47

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 the spec/spec_helper.rb:

if ENV["DEBUG"]
  require 'pry-byebug'
  # and any other Pry extensions etc
  binding.pry
end

Then if I want to using debugging, I run env DEBUG=1 bin/rackup config.ru or env DEBUG=1 bin/rspec (I use it a lot with the -e switch in RSpec) and then set break points using break. It means I don't have to change the code at all to drop into it.

查看更多
\"骚年 ilove
5楼-- · 2019-01-31 07:59

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.

enter image description here

Here is the repo: https://github.com/hlee/sinatra_debugger_example

查看更多
登录 后发表回答