Trace the code in Ruby? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionIs开发者_运维知识库 there a good way to run a script and see all the steps:
- Which code is executed
- Which file the code is in
- What it returns
- The error messages
This would be a good way to learn how an open source project is working.
Aren't there solutions like this for Ruby?
Eg.
require "httparty"
HTTParty.get "http://www.google.se"
Then it would run the code and show me all the code it is executing, in which file and line, returned objects, error messages etc.
Tracer might do it for you http://en.wikibooks.org/wiki/Ruby_Programming/Standard_Library/Tracer
if you want to just see exceptions raised, then run ruby -d (or use ruby-debug and "Catch" Exception)
There is Kernel#set_trace_func
which mostly covers what you require:
proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. proc is invoked whenever an event occurs.
I'm not sure, though, what do you mean by "the error messages". If you refer to exceptions, if you don't handle them in your code, your code will terminate with the exception printed.
use a debugger? ruby comes with one built in, or use ruby-debug
You can raise an Exception, catch it and then use backtrace method of an Exception object.
精彩评论