When I'm running a simple Ruby script, what's the easiest way to dump an object's fields to the console?
I'm looking for something similar to PHP's print_r()
that will work with arrays as well.
When I'm running a simple Ruby script, what's the easiest way to dump an object's fields to the console?
I'm looking for something similar to PHP's print_r()
that will work with arrays as well.
Ruby doc for
p
.If you're looking for just the instance variables in the object, this might be useful:
or as a one-liner for copy and pasting:
If you want to print an already indented JSON:
The
to_yaml
method seems to be useful sometimes:returns
(Does this depend on some
YAML
module being loaded? Or would that typically be available?)puts foo.to_json
might come in handy since the json module is loaded by default
Possibly: