撬:告诉我堆栈

问题描述 投票:83回答:5

使用Pry in Rails,当我在代码binding.pry中遇到断点时

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

ruby-on-rails pry
5个回答
45
投票

使用pry-stack_explorer插件,它允许您在调用堆栈中上下移动(使用updown),显示callstack(使用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>)> 

107
投票

要做到这一点,没有任何撬插件(我有pry-stack_explorer的麻烦),只需看看caller

我实际上寻找我的项目名称来过滤掉所有不相关的rails堆栈项目。例如,如果我的项目名称是archie,我会使用:

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

这给了我正在寻找的堆栈跟踪。

一个较短的方法是:

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

哪个也适用。


80
投票

pry-backtrace显示了Pry会话的回溯。

还有wtf?。哪个节目是最近一次异常的回溯。添加更多问号以查看更多回溯或感叹号以查看全部内容。

在pry中键入help以查看所有其他命令:)


1
投票

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

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


0
投票

延伸保罗奥利弗的答案。

如果您有要永久排除的短语列表,则可以使用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

调用qazxsw poi将导致过滤的qazxsw poi输出。 callerf周围的奇怪迹象正在着色,以复制caller的原始外观。我从#{output}那里拿了颜色。

或者,如果您不想创建自定义命令,请使用caller搜索命令历史记录。

© www.soinside.com 2019 - 2024. All rights reserved.