igneus / calendarium-romanum

liturgical calendar library (Roman Catholic, post-Vatican II)
50 stars 21 forks source link

Season#to_s, Colour#to_s #17

Closed igneus closed 6 years ago

igneus commented 6 years ago

This is something worth a discussion before it gets implemented:

Season and Colour currently lack proper implementation of #to_s and the defaults are rather unsatisfying, e.g.

#<CalendariumRomanum::Season:0x00000001540e88>

What would be the most convenient (most convenient for a Rubyist) return value? Is Rank#to_s the way to go (return a human-friendly name of the rank translated to the current locale) or should this be changed, too?

igneus commented 6 years ago

Possible return values I can think of:

igneus commented 6 years ago

Examples of behaviour of #to_s in built-in classes:

igneus commented 6 years ago

I realized this is quite a general question of Ruby best practices, so I summarized it in a post to the ruby-lang mailing list and received some helpful feedback.

To the response by Matthew Kerwin we can reply that seasons, colours and ranks are not intended to be directly printed and by their nature they are not "something like String".

I find the rules suggested by Victor Shepelev especially helpful:

  1. #to_s should show the content of the object, when relevant (for your case, it is relevant, e.g. Lent should be seen somewhere);
  2. For value objects, there is no sense in showing object id.
  3. Unless object is kinda string (e.g. will most probably be used in situations like [list, of objects].join(' - ') or something), I prefer to stick to # convention, I am not sure what is most appropriate for your case
  4. Finally, #to_s should be short and informative enough to play well in the cases like raise ArgumentError, "Expected numeric, #{object} got"

So, considering all above, it is either #, or (if and only if you 100% sure it will be massively used in text rendering) just "red".