log(JSON. stringify(obj)) method can be useful for logging the object to the console as string, as long as the data in the object is JSON-safe. For complex objects, the method Object. entries(obj) is a way of looping through an object that can be used to log the object to the console.
Dumping objects is a great way to visualize values and types while enabling you to easily debug and detect problems at runtime. This is particularly important whenever you don't have access to specific debugging tools.
Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.
How to Print a dart object to console in Dart. When you are accessing an instance variable of an object, the default toString() method returns the string and returns the Instance of 'Employee'. So, You override the toString() method in a class and print an object.
Possibly:
puts variable.inspect
You might find a use for the methods
method which returns an array of methods for an object. It's not the same as print_r
, but still useful at times.
>> "Hello".methods.sort
=> ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "[]=", "__id__", "__send__", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "class", "clone", "collect", "concat", "count", "crypt", "delete", "delete!", "detect", "display", "downcase", "downcase!", "dump", "dup", "each", "each_byte", "each_line", "each_with_index", "empty?", "entries", "eql?", "equal?", "extend", "find", "find_all", "freeze", "frozen?", "grep", "gsub", "gsub!", "hash", "hex", "id", "include?", "index", "inject", "insert", "inspect", "instance_eval", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "intern", "is_a?", "is_binary_data?", "is_complex_yaml?", "kind_of?", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "member?", "method", "methods", "min", "next", "next!", "nil?", "object_id", "oct", "partition", "private_methods", "protected_methods", "public_methods", "reject", "replace", "respond_to?", "reverse", "reverse!", "rindex", "rjust", "rstrip", "rstrip!", "scan", "select", "send", "singleton_methods", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "taguri", "taguri=", "taint", "tainted?", "to_a", "to_f", "to_i", "to_s", "to_str", "to_sym", "to_yaml", "to_yaml_properties", "to_yaml_style", "tr", "tr!", "tr_s", "tr_s!", "type", "unpack", "untaint", "upcase", "upcase!", "upto", "zip"]
The to_yaml
method seems to be useful sometimes:
$foo = {:name => "Clem", :age => 43}
puts $foo.to_yaml
returns
---
:age: 43
:name: Clem
(Does this depend on some YAML
module being loaded? Or would that typically be available?)
p object
Ruby doc for p
.
p(*args) public
For each object, directly writes obj.inspect followed by a newline to the program’s standard output.
If you're looking for just the instance variables in the object, this might be useful:
obj.instance_variables.each do |var|
puts [var, obj.instance_variable_get(var).inspect].join(":")
end
or as a one-liner for copy and pasting:
obj.instance_variables.each{ |var| puts [var, obj.instance_variable_get(var).inspect].join(":")}
puts foo.to_json
might come in handy since the json module is loaded by default
If you want to print an already indented JSON:
require 'json'
...
puts JSON.pretty_generate(JSON.parse(object.to_json))
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