Ruby/Rails Debugging Tips & Tricks

Igor Kasyanchuk
Jan 7, 2023 • 1 minute read
  • code debugging using global variables

    Imagine that you want to have a breakpoint at a specific place, but this method is executed many times, or you want to have better control when execution should be stopped. Could be useful when you debug specs or methods which are executed multiple times.

    You can use this approach:

    # logic
    def method_1
      ...code...
      binding.pry if $x
      ...code...
    end
    
    # place where you set this flag, could be inside spec
    def method_2
      ...code...
      $x = true if rule?
      ...code...
    end
  • method caller

    puts caller to display the current call stack. Could be useful if you need to understand from some method is called.

  • pry commands

    Pry tips: show-method, ls, cd <method>, whereami, wtf (to see the last exception)

Useful gems

See all