2013-03-08 41 views
69

Rails中使用撬,當我打的代碼中設置斷點 binding.pry撬:給我疊

我想知道我是怎麼來到這裏,誰罵我,誰罵他們,等等。但奇怪的是我看不到那個命令。有人知道嗎?

回答

41

使用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>)> 
75

pry-backtrace這是顯示Pry會話的回溯。

還有wtf?。哪個節目是最近一次例外的回溯。添加更多問號以查看更多回溯或感嘆號以查看全部內容。

在撬類型help看到所有的其他命令:)

+1

'撬backtrace'是好的,但'撬stack_explorer'插件的方式更強大的(雖然它是另一種寶石,一個插件) – horseyguy 2013-03-09 02:10:27

+7

但事實是,有時你不使用所有這些功能:) – nXqd 2014-09-30 02:03:53

+3

謝謝我不知道wtf?命令! – 2016-03-23 15:51:26

66

要做到這一點,沒有任何撬插件(我是有撬stack_explorer煩惱),只是看caller

我實際上在尋找我的項目名稱來篩選掉所有不相關的鐵軌棧項目。例如,如果我的項目名稱是archie我會使用:

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

這給了我堆棧跟蹤我要找的。

更短的方法是:

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

哪些作品一樣好。

+0

這太好了。我很惱火,因爲它包含了pry調用堆棧,我只是想從我的應用程序中獲得特別的東西。 +1! – cdpalmer 2015-07-08 21:55:56

+3

完美。我添加了一個key-combo給tmux來輸入這個(bind'B'send-keys'...^M'),而不是使用「reject」,所以它更通用:'''caller.reject {| x | x [「vendor/bundle」] || x [「/。rbenv/versions /」]} – hoodslide 2015-07-09 19:15:06

+2

對於Ruby社區來說,只有有用的答案被埋在建議之下才能安裝一些插件。 – 2016-01-21 20:38:54

1

您可以使用已在gem庫中定義的調用方法。該方法的返回值將是一個數組。所以你可以在這堆行中事件應用數組搜索方法

下面這個對於強大的跟蹤也很有幫助。 https://github.com/pry/pry-stack_explorer