如何将 Pry 与 Sinatra 一起使用? [英] How to use Pry with Sinatra?

查看:52
本文介绍了如何将 Pry 与 Sinatra 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个 Sinatra 应用程序,并想使用 Pry 来检查/调试应用程序中发生的一些事情.我之前也没有使用过 Pry,但我想尝试一下.我如何开始在 Sinatra 应用程序中使用 Pry?

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?

推荐答案

总结

  1. 在应用程序顶部使用 require 'pry'.
  2. 每当您想进入交互式会话时,请在您的代码中调用 binding.pry.有关使用 Pry 的信息,请参阅 TurningIRB 使用 PryPry wiki.
  3. 当您完成特定的交互式会话后,输入 exit 或 Ctrl-D;Sinatra 将在停止的地方继续运行.
  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.

示例

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}!

这是我启动 Web 服务器时的样子:

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

当我在 Web 浏览器中向 http://localhost:4567 发出请求时,控制台会进入 Pry 调试器在发送结果之前:

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

进一步调试

如果您希望能够使用传统的调试命令,例如设置基于行的断点,或单步执行,或在引发异常时中断,请参阅 PryDebug 库,来自 Mon-Ouie.

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.

这篇关于如何将 Pry 与 Sinatra 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆