Why do this Ruby object have both to_s and inspect methods that appear to do the same thing?
inspect
is a method that, by default, tells you the class name, the instance's object_id
, and lists off the instance's instance variables.
print
and puts
are used, as you already know, to put the value of the object's to_s
method to STDOUT
. As indicated by Ruby's documentation, Object#to_s
returns a string representing the object -- used for end-user readability.
print
and puts
are identical to each other except for puts
automatically appends a newline, while print
does not.