129

Using Pry in Rails, when I hit a breakpoint in the code 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?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
pitosalas
  • 10,286
  • 12
  • 72
  • 120

5 Answers5

178

To do this without any pry plugins (I was having troubles with pry-stack_explorer), just look at 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" }

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.

amenthes
  • 3,117
  • 1
  • 32
  • 38
Paul Oliver
  • 7,531
  • 5
  • 31
  • 34
  • 1
    This is great. I was annoyed because it included the pry call stack and I just wanted what was specifically coming from my application. +1! – cdpalmer Jul 08 '15 at 21:55
  • 7
    Perfect. I added a key-combo to tmux to enter this (bind 'B' send-keys '...^M'), using a "reject" instead so it's more generic: ```caller.reject {|x| x["vendor/bundle"] || x["/.rbenv/versions/"] }``` – hoodslide Jul 09 '15 at 19:15
  • 6
    True to form for the Ruby community, the only useful answer is buried underneath advice to go install some plugins. – Jesse Dhillon Jan 21 '16 at 20:38
  • 4
    this answer deserves so many upvotes. Yes, you _can_ install more stuff on top of pry. But you could also use ruby's existing language features to get almost as far (certainly far enough to answer the OP's question!) – amenthes Aug 04 '16 at 08:44
  • 3
    This answer should be the the correct one as it requires no additional plugins! – Alvaro Cavalcanti Nov 15 '19 at 11:35
  • 1
    How can I make this a method I can call from the pry console? so instead of `caller.select {|line| line.include? "myapp" }` just call `my_stack` for example.. – pixelearth Feb 07 '20 at 20:50
  • @pixelearth, look for sloneorzeszki's answer for how to do that. – Paul Oliver Feb 09 '20 at 05:32
93

There is pry-backtrace which show's the backtrace for the Pry session.

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.

Type help in pry to see all the other commands :)

gef
  • 7,025
  • 4
  • 41
  • 48
61

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:

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>)> 
horseyguy
  • 29,455
  • 20
  • 103
  • 145
  • would just like to add that you can go up and down the stack with a number next to it, like: `up 9` – nabais Aug 19 '21 at 09:33
3

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.

In ~/.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 callerf will result in a filtered caller output. Weird signs around #{output} is coloring to replicate the original look of the caller. I took the color from here.

Alternatively, if you don't want to make a custom command, use Ctrl+R to search through command history.

sloneorzeszki
  • 1,274
  • 3
  • 12
  • 22
1

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

Below is also helpful for powerful trace. https://github.com/pry/pry-stack_explorer

Nishant Upadhyay
  • 639
  • 7
  • 20