我安装pry并尝试使用它。
gem "pry"
require 'pry'
#..........
binding.pry
但有3个问题:
pry(#)> Error: execution expired /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `getbyte' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `readline' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `block in readline' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:544:in `handle_read_errors' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:589:in `readline' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:369:in `retrieve_line' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:304:in `block in r' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:301:in `loop' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:301:in `r' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:271:in `re' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:251:in `rep' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:231:in `block (3 levels) in repl' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:230:in `loop' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:230:in `block (2 levels) in repl' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:229:in `catch' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:229:in `block in repl' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:228:in `catch' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:228:in `repl' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_class.rb:154:in `start' /var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/core_extensions.rb:22:in `pry' /home/alex/Documents/ruby/my_projects/controllers/my_controller.rb:24:in `block in ' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!' /home/alex/Documents/ruby/my_projects/app.rb:21:in `block in ' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call' /var/lib/gems/1.9.1/gems/rack-flash3-1.0.1/lib/rack/flash.rb:120:in `call' /var/lib/gems/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `block in call' /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout' /var/lib/gems/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call' /var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call' /var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call' /var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call' /var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call' /var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:129:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/showexceptions.rb:21:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:64:in `block in call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `each' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call' /var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/chunked.rb:43:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call' /var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' pry(#)>
好的,我安装了
gem "debugger"
并添加断点
debugger
一切都没有发生。
日志文件不是我想要的。我只是想调试一个Sinatra应用程序。我该怎么做?
答案 0 :(得分:6)
在你的Gemfile中:
gem "pry"
gem "pry-nav"
运行捆绑包。然后,只要脚本遇到binding.pry
,您就会打开一个shell。您可以使用whereami
查看自己的位置,使用next
移至下一行,继续使用continue
运行脚本。如果要检查变量,只需键入其名称。
有关详细信息,请参阅pry-nav。
答案 1 :(得分:3)
pry-debugger更好。你需要在Modular风格下运行sinatra app。
我在github上创建了一个repo,它解释了如何使用pry进行调试。
以下是回购:https://github.com/hlee/sinatra_debugger_example
如果你检查pry-debugger的源代码,你会看到:
# pry-debugger.gemspec
# Dependencies
gem.required_ruby_version = '>= 1.9.2'
gem.add_runtime_dependency 'pry', '>= 0.9.10'
gem.add_runtime_dependency 'debugger', '~> 1.3'
gem.add_development_dependency 'pry-remote', '~> 0.1.6'
这意味着,pry-debugger仅支持MRI 1.9.2和1.9.3。由于它依赖于调试器,因此Pry-debugger将支持更快的跟踪,断点等。
另一方面,这是pry-nav来源:
# pry-nav.gemspec
# Dependencies
gem.required_ruby_version = '>= 1.8.7'
gem.add_runtime_dependency 'pry', '~> 0.9.10'
gem.add_development_dependency 'pry-remote', '~> 0.1.6'
其中meas,pry-nav是一种纯粹的ruby方法,不依赖于调试器。将支持1.8.7,不支持断点和调试器的其他一些功能。
break SomeClass#run # Break at the start of `SomeClass#run`.
break Foo#bar if baz? # Break at `Foo#bar` only if `baz?`.
break app/models/user.rb:15 # Break at line 15 in user.rb.
break 14 # Break at line 14 in the current file.
break --condition 4 x > 2 # Change condition on breakpoint #4 to 'x > 2'.
break --condition 3 # Remove the condition on breakpoint #3.
break --delete 5 # Delete breakpoint #5.
break --disable-all # Disable all breakpoints.
break # List all breakpoints. (Same as `breakpoints`)
break --show 2 # Show details about breakpoint #2.
pry-nav和pry-debugger无法一起加载。