让我看看那堆东西

在 Rails 中使用 Pry,当我在代码中碰到一个断点时 捆绑,刺探

我想知道我是怎么到这里的,谁给我打的电话,谁给他们打的电话,等等。但奇怪的是,我没有看到这个命令。有人知道吗?

61484 次浏览

这是 pry-backtrace,它显示了 Pry 会话的回溯跟踪。

还有 卧槽?。这是最近一次异常的回溯。添加更多的问号来查看更多的回溯,或者添加一个叹号来查看全部内容。

Type 帮助 in pry to see all the other commands :)

Use the pry-stack_explorer plugin, it allows you to move up and down the call-stack (with up and down), display the callstack (with show-stack), and so on:

看这里:

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>)>

要在没有任何 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 archie I'd use:

caller.select {|line| line.include? "archie" }

这就是我要找的堆栈痕迹。

更简单的办法是:

caller.select {|x| x["archie"] }

这样也好。

您可以使用已经在 gem 库中定义的调用方法。该方法的返回值将是一个数组。因此,您可以事件应用数组方法来搜索这些行

下面对于强大的跟踪也是有帮助的。 https://github.com/pry/pry-stack_explorer

扩展保罗 · 奥利弗的回答。

如果你有一个短语列表,你想永久排除,你可以做到这一点与自定义命令功能在 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的原始外观。我从 给你中提取了颜色。

或者,如果不想创建自定义命令,可以使用 Ctrl+R搜索命令历史记录。