Whenever I create a ruby object, I always try to provide a useful to_s. I’ve noticed that most people — especially in Rails and other Gems that I use — don’t do this. This is not a complaint or a judgment. I’ve just always been curious. I’m sure there is a very good reason and I’d like to know what it is.
Sometimes I make the to_s something that would be useful in a UI, so I can type, for example, #{user} instead of #{user.name}. More often, it’s something that would be useful when debugging, whether in the console or in a debugger, to tell me what objects are in an array or what’s the current state of an object.
For example, I often wish that ActiveModel::Errors would tell me why my model is invalid (e.g. ‘name can’t be blank’) instead of “#ActiveModel::Errors:0x00007f89993af938”.
Is there a reason that folks don’t provide a useful to_s?
Agree with you 100%. I usually provide some sort of reasonable to_s method so when using in string interpolation, ERB templates, etc I can just reference the object and get something that looks reasonable. If the object is an ActiveRecord object the alias_attribute method is useful for this. For example:
class User < ApplicationRecord
alias_attribute :to_s, :name
end
I do want to note that I think to_s should be reserved for outputting something reasonable for a user to digest. You mentioned making it useful for debugging. I would suggest if you want to improve the debugging output you override inspect as that is the method consoles general call to stringify an object.