Ruby Debugging Tips - caller, raise, exit!

I had problems running the examples in the Aaron's original blog post. I have rewritten the examples so that it works for me.

I know where I am. But I don't know how I got here

class Foo
  def bar
    puts 'I was called by:'
    puts caller
  end
end

f = Foo.new
f.bar

The output:

I was called by:
untitled 3:10: in `<main>'

I want to print the stack trace once.

The exit! will print the stack trace.

class Foo
  def bar
    puts 'I was called by:'
    puts caller
    exit!
  end
end

f = Foo.new
f.bar

The output:

I was called by:
untitled 3:11: in `<main>'
Program exited with code #1 after 0.44 seconds.

The raise will also print the stack trace.

class Foo
  def bar
    puts 'I was called by:'
    puts caller
    raise
  end
end

f = Foo.new
f.bar
I was called by:
untitled 3:10: in `<main>'

RuntimeError:

method bar  in untitled 3 at line 5
method <main>   in untitled 3 at line 10

Program exited with code #1 after 0.43 seconds.

An example that shows just the raise. The raise will tell you who called you.

class Foo
  def bar
    raise
  end
end

f = Foo.new
f.bar

The output:

RuntimeError:

method bar  in untitled 3 at line 3
method <main>   in untitled 3 at line 8
Program exited with code #1 after 0.41 seconds.

Reference

I am a puts debuggerer


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.