【问题标题】:Pry: show me the stack撬:给我看堆栈
【发布时间】:2013-02-24 12:50:33
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails pry


    【解决方案1】:

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

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

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

    【讨论】:

    • pry-backtrace 没问题,但pry-stack_explorer 插件功能更强大(尽管它是另一个 gem,一个插件)
    • 但事实是有时您不会使用所有这些功能 :)
    【解决方案2】:

    使用pry-stack_explorer插件,它可以让你上下移动调用栈(updown),显示调用栈(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
    【解决方案3】:

    要在没有任何 pry 插件的情况下执行此操作(我在使用 pry-stack_explorer 时遇到了问题),只需查看 caller

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

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

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

    更短的方法是:

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

    同样有效。

    【讨论】:

    • 这很棒。我很生气,因为它包含了 pry 调用堆栈,而我只想要来自我的应用程序的具体内容。 +1!
    • 完美。我在 tmux 中添加了一个组合键来输入它(绑定 'B' 发送键 '...^M'),而是使用“拒绝”,所以它更通用:caller.reject {|x| x["vendor/bundle"] || x["/.rbenv/versions/"] }
    • 真正适合 Ruby 社区,唯一有用的答案隐藏在安装一些插件的建议之下。
    • 这个答案值得这么多赞成。是的,您可以在 pry 之上安装更多东西。但是您也可以使用 ruby​​ 现有的语言功能来达到几乎一样远(肯定足以回答 OP 的问题!)
    • 这个答案应该是正确的,因为它不需要额外的插件!
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      扩展 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 系统上的主文件夹。
      猜你喜欢
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2013-02-09
      • 1970-01-01
      • 2018-07-01
      相关资源
      最近更新 更多