Ruby-on-rails 撬:给我看堆栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15303103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pry: show me the stack
提问by pitosalas
Using Pry in Rails, when I hit a breakpoint in the code binding.pry
在 Rails 中使用 Pry,当我在代码 binding.pry 中遇到断点时
I want to know how I got here, who called me, who called them, etc. But oddly I don't see that command. Does anyone know?
我想知道我是怎么到这里的,谁给我打电话的,谁给他们打电话的,等等。但奇怪的是我没有看到那个命令。有人知道吗?
采纳答案by horseyguy
Use the pry-stack_explorerplugin, it allows you to move up and down the call-stack (with upand down), display the callstack (with show-stack), and so on:
使用pry-stack_explorer插件,它允许您上下移动调用堆栈(使用up和down),显示调用堆栈(使用show-stack),等等:
see here:
看这里:
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>)>
回答by Paul Oliver
To do this without any pry plugins (I was having troubles with pry-stack_explorer), just look at caller.
要在没有任何 pry 插件的情况下执行此操作(我在使用 pry-stack_explorer 时遇到了麻烦),只需查看caller.
I actually look for my project name to filter out all the irrelevant rails stack items. For example, if my project name were archieI'd use:
我实际上是在寻找我的项目名称来过滤掉所有不相关的 Rails 堆栈项。例如,如果我的项目名称是archie我会使用:
caller.select {|line| line.include? "archie" }
Which gives me the stack trace I'm looking for.
这给了我正在寻找的堆栈跟踪。
A shorter way would be:
更短的方法是:
caller.select {|x| x["archie"] }
Which works just as well.
这也同样有效。
回答by gef
There is pry-backtracewhich show's the backtrace for the Pry session.
有pry-backtrace显示了 Pry 会话的回溯。
There is also wtf?. Which show's the backtrace of the most recent exception. Add more question marks to view more of the backtrace or an exclamation mark to see it all.
还有wtf?. 哪个节目是最近异常的回溯。添加更多问号以查看更多回溯或添加感叹号以查看全部内容。
Type helpin pry to see all the other commands :)
在 pry 中键入help以查看所有其他命令:)
回答by Nishant Upadhyay
You can use caller method which already defined inside the gem library. The return value of that method will be an array. So you can event apply array methods for search in that bunch of lines
您可以使用已经在 gem 库中定义的调用者方法。该方法的返回值将是一个数组。所以你可以事件应用数组方法来搜索那一堆行
Below is also helpful for powerful trace. https://github.com/pry/pry-stack_explorer
下面也有助于强大的跟踪。 https://github.com/pry/pry-stack_explorer
回答by sloneorzeszki
Extending on Paul Oliver's answer.
扩展保罗奥利弗的回答。
If you have a list of phrases you want to permanently exclude you can do that with a custom commands feature in Pry.
如果您有要永久排除的短语列表,您可以使用 Pry 中的自定义命令功能来实现。
In ~/.pryrc:
在~/.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
Calling callerfwill result in a filtered calleroutput. Weird signs around #{output}is coloring to replicate the original look of the caller. I took the color from here.
调用callerf将导致过滤caller输出。周围奇怪的标志#{output}正在着色以复制caller. 我从这里取了颜色。
Alternatively, if you don't want to make a custom command, use Ctrl+Rto search through command history.
或者,如果您不想创建自定义命令,请使用Ctrl+R搜索命令历史记录。

