October 2006
Intermediate to advanced
888 pages
16h 55m
English
At first glance, the to_s and to_str methods seem confusing. They both convert an object into a string representation, don’t they?
There are several differences. First, any object can in principle be converted to some kind of string representation; that is why nearly every core class has a to_s method. But the to_str method is never implemented in the core.
As a rule, to_str is for objects that are really very much like strings—that can “masquerade” as strings. Better yet, think of the short name to_s as being explicit conversion and the longer name to_str as being implicit conversion.
You see, the core does not define any to_str methods (that I am aware of). But core methods do call to_str sometimes (if ...
Read now
Unlock full access