Question

Sometimes when I'm working in the rails console, I find I want to step through a particular method (from my rails app) in the debugger. In the past I have done this by temporarily adding a debugger statement to the source code of the method, then calling that method from the console.

Is there a way I can "step into" a method from the console, without editing its source code?

This would be particularly nice on a shared development server, so that I wouldn't need to throw in random debugger statements with vi and remember to remove them later.

I tried the following but not surprisingly it doesn't work:

$ rails c --debugger
=> Debugger enabled
Loading development environment (Rails 3.2.13)
irb(main):001:0> def startdebug
irb(main):002:1>   debugger
irb(main):003:1>   MyModel.last.my_method
irb(main):004:1> end
=> nil
irb(main):005:0> startdebug

It says

*** No sourcefile available for (irb)

And soon I end up stepping through irb code rather than my_method.

Was it helpful?

Solution

mhm this sound a bit like metaprogramming for me, you may inject a debugging method at runtime in your model, which will add the debugging statement just before the method call so you can inspect the call as needed, like:

m = MyModel.last
m.class.send(:define_method, :debug_my_method){debugger; my_method}
m.debug_my_method

this should do on the irb (just tested it on may rails console)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top