shiningthrough

Simple debugging in ruby on rails

24 April 2007

I often find while writing a ruby on rails applications that i need to display the contents of a variable quickly and easily, this is straight forward in a view, but not so easy in a controller or model. There are a couple of ways to do this:

logger.debug "variable"

will write to the development.log file, or if your using webrick as your development server, you can use:

STDERR.puts "variable"

and it will show up on the console output where your running your server.

Comments

Josh

I use breakpoints, they are very powerfull. Check out this: http://wiki.rubyonrails.org/rails/pages/HowtoDebugWithBreakpoint

shiningthrough

Thanks Josh, yes that is indeed another option, ill add it to the list.

Erhard

I just put a put(variable). You can see it in the console.

M

Instead of STDERR.puts "variable", just use p

0x4a6f4672

The Logger can be used with different log levels, the levels available on the logger are in ascending order: debug, info, warn, error, and fatal. Depending on your Rails environment (development, staging or production), the "debug" may not be enough. You might also want to use variable.inspect to display the attributes of an object, see also http://guides.rubyonrails.org/debugging_rails_applications.html