to_s is one of the most useful methods in Ruby. Almost every class you ever write should have an implementation of it; it’s useful when debugging, it’s useful when outputting information for people.
But sometimes you call
puts @my_object and it ignores your
to_s method and just returns the object ID; something like
Why? How do you make it work right?
Well the trick is that
to_s means “to string” – your implementation has to return a string object, otherwise Ruby will ignore it and do its own thing (showing you the object ID).
So always make sure you return a string object and all will be well.