[HN Gopher] Ruby Type Conversion
___________________________________________________________________
Ruby Type Conversion
Author : wildmaples
Score : 18 points
Date : 2021-09-10 20:32 UTC (2 hours ago)
(HTM) web link (kddnewton.com)
(TXT) w3m dump (kddnewton.com)
| vidarh wrote:
| It seems the author might be unaware of the semantic difference
| expected for to_s vs. to_str and equivalent for other short vs
| long named conversions.
|
| The former is expected to be pretty imprecise. E.g. if an object
| can somehow he described with a string it's ok to implement to_s.
|
| But you should only implement to_str if an object can be
| naturally represented as a String. E.g. if you implement a
| specialized string class, implementing to_str is fine, while your
| Person class might reasonably return a persons name from to_s but
| probably shouldn't implement to_str.
| kddeisz wrote:
| Is that semantic difference documented anywhere? I've never
| heard of this before, would love to learn more.
| dragonwriter wrote:
| The official documentation for the implicit conversion
| protocol (which to_str is part of, as distinct from to_s) is
| here:
|
| https://ruby-
| doc.org/core-3.0.2/doc/implicit_conversion_rdoc...
|
| to_s is documented here:
|
| https://ruby-doc.org/core-3.0.2/Object.html#method-i-to_s
| [deleted]
___________________________________________________________________
(page generated 2021-09-10 23:01 UTC)