The .inspect
method of any object should format is correctly for display, just do..
<%= theobject.inspect %>
The .methods
method may also be of use:
<%= theobject.methods.inspect %>
It may help to put that in <pre>
tags, depending on the data
In views:
include DebugHelper
...your code...
debug(object)
In controllers, models, and other code:
puts YAML::dump(object)
Source
In a view you can use <%= debug(yourobject) %>
which will generate a YAML view of your data. If you want something in your log you should use logger.debug yourobject.inspect
.
You can also use YAML::dump shorthand (y) under Rails console:
>> y User.first
--- !ruby/object:User
attributes:
created_at: 2009-05-24 20:16:11.099441
updated_at: 2009-05-26 22:46:29.501245
current_login_ip: 127.0.0.1
id: "1"
current_login_at: 2009-05-24 20:20:46.627254
login_count: "1"
last_login_ip:
last_login_at:
login: admin
attributes_cache: {}
=> nil
>>
If you want to just preview some string contents, try using raise (for example in models, controllers or some other inaccessible place). You get the backtrace for free:)
>> raise Rails.root
RuntimeError: /home/marcin/work/github/project1
from (irb):17
>>
I also really encourage you to try ruby-debug:
It's incredibly helpful!
You can use puts some_variable.inspect
. Or the shorter version: p some_variable
. And for prettier output, you can use the awesome_print gem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With