Published in: Ruby
URL: http://whytheluckystiff.net/articles/rubyOneEightOh.html
With to_str, Matz is giving us a simpler way of demonstrating that our classes can be used as strings directly... So why not use to_s? Because to_s coerces objects into strings. So, to_str is an implicit cast, whereas to_s is an explicit cast... ...[T]here is a reason that to_s is shorter. First, it implies that the object isn't really much of a string, so we're only using the first letter... With to_str, we're tagging an object as much closer to being a string, so we give it the first three letters. It's almost half of a string!
You need to login to post a comment.
