【发布时间】:2013-02-24 12:50:33
【问题描述】:
在 Rails 中使用 Pry,当我在代码中遇到断点时 绑定.pry
我想知道我是怎么到这里的,谁给我打电话,谁给他们打电话等等。但奇怪的是我没有看到那个命令。有人知道吗?
【问题讨论】:
标签: ruby-on-rails pry
在 Rails 中使用 Pry,当我在代码中遇到断点时 绑定.pry
我想知道我是怎么到这里的,谁给我打电话,谁给他们打电话等等。但奇怪的是我没有看到那个命令。有人知道吗?
【问题讨论】:
标签: ruby-on-rails pry
pry-backtrace 显示了 Pry 会话的回溯。
还有wtf?。哪个节目是最近异常的回溯。添加更多问号以查看更多回溯或添加感叹号以查看全部内容。
在 pry 中键入 help 以查看所有其他命令 :)
【讨论】:
pry-backtrace 没问题,但pry-stack_explorer 插件功能更强大(尽管它是另一个 gem,一个插件)
使用pry-stack_explorer插件,它可以让你上下移动调用栈(up和down),显示调用栈(show-stack)等等:
看这里:
Frame number: 0/64
From: /Users/johnmair/ruby/rails_projects/personal_site/app/controllers/posts_controller.rb @ line 7 PostsController#index:
5: def index
6: @posts = Post.all
=> 7: binding.pry
8: end
[1] pry(#<PostsController>)> show-stack
Showing all accessible frames in stack (65 in total):
--
=> #0 index <PostsController#index()>
#1 [method] send_action <ActionController::ImplicitRender#send_action(method, *args)>
#2 [method] process_action <AbstractController::Base#process_action(method_name, *args)>
#3 [method] process_action <ActionController::Rendering#process_action(*arg1)>
<... clipped ...>
[2] pry(#<PostsController>)> up
Frame number: 1/64
Frame type: method
From: /Users/johnmair/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb @ line 4 ActionController::ImplicitRender#send_action:
3: def send_action(method, *args)
=> 4: ret = super
5: default_render unless response_body
6: ret
7: end
[3] pry(#<PostsController>)>
【讨论】:
up 9
要在没有任何 pry 插件的情况下执行此操作(我在使用 pry-stack_explorer 时遇到了问题),只需查看 caller。
我实际上是在寻找我的项目名称来过滤掉所有不相关的 Rails 堆栈项。例如,如果我的项目名称是 archie,我会使用:
caller.select {|line| line.include? "archie" }
这给了我正在寻找的堆栈跟踪。
更短的方法是:
caller.select {|x| x["archie"] }
同样有效。
【讨论】:
caller.reject {|x| x["vendor/bundle"] || x["/.rbenv/versions/"] }
您可以使用已在 gem 库中定义的调用方方法。该方法的返回值将是一个数组。因此,您可以事件应用数组方法以在那一堆行中进行搜索
下面也有助于强大的跟踪。 https://github.com/pry/pry-stack_explorer
【讨论】:
扩展 Paul Oliver 的回答。
如果您有一个想要永久排除的短语列表,您可以使用 Pry 中的自定义命令功能来做到这一点。
在~/.pryrc:
Pry::Commands.block_command "callerf", "Filter the caller backtrace" do
output = caller.reject! { |line| line["minitest"] || line["pry"] }
puts "\e[31m#{output.join("\n")}\e[0m"
end
调用callerf 将产生过滤后的caller 输出。 #{output} 周围的奇怪标志正在着色以复制 caller 的原始外观。我从here 获取颜色。
或者,如果您不想创建自定义命令,请使用Ctrl+R 搜索命令历史记录。
【讨论】:
~/.pryrc。如果没有,只需创建它。 ~/ 始终表示 Unix 系统上的主文件夹。