Posted By

cczona on 03/13/08


Tagged

ruby String tos tostr


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


to_str vs to_s


 / Published in: Ruby
 

URL: http://whytheluckystiff.net/articles/rubyOneEightOh.html

  1. 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...
  2.  
  3. ...[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...
  4.  
  5. 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!

Report this snippet  

You need to login to post a comment.