Forum: Ruby let my custom defined class convert to string?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2008-11-18 03:04
Is there any way I can make it so my custom-defined class can be
automatically converted to a string?

I have a #to_s method in it.

So it works when I do something like "#{var_of_my_class} foo".  It calls
#to_s there.

But if I try instead "some string" + var_of_my_class, then ruby
complains that it can't convert MyClass to a string.

Is there anything I can do so my class will always be converted properly
to a String by ruby, in all cases, so client code can really use it just
like a string?

Jonathan
87349a7a95b3f2e83c20194ef122885c?d=identicon&s=25 Einar Magnús Boson (Guest)
on 2008-11-18 03:37
(Received via mailing list)
On 18.11.2008, at 02:01 , Jonathan Rochkind wrote:

> complains that it can't convert MyClass to a string.
>
class MyClass < String

end


Einar Magnús Boson
+354-661 1649
einarmagnus@tistron.se
einar.boson@gmail.com
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-11-18 08:46
(Received via mailing list)
Alle Tuesday 18 November 2008, Jonathan Rochkind ha scritto:
>
> Is there anything I can do so my class will always be converted properly
> to a String by ruby, in all cases, so client code can really use it just
> like a string?
>
> Jonathan

Defining a to_str method makes String#+ work,  but I'm not sure it'll
work
everywhere. According to some documentation I found on google, defining
that
method should make the class usable in place of a string. Note, however,
that
you still have to define a to_s method if you want to change the output
of
puts.

I hope this helps

Stefano
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2008-11-18 16:07
Thanks, that helps a lot, actually!

Jonathan

Stefano Crocco wrote:
> Alle Tuesday 18 November 2008, Jonathan Rochkind ha scritto:
>>
>> Is there anything I can do so my class will always be converted properly
>> to a String by ruby, in all cases, so client code can really use it just
>> like a string?
>>
>> Jonathan
>
> Defining a to_str method makes String#+ work,  but I'm not sure it'll
> work
> everywhere. According to some documentation I found on google, defining
> that
> method should make the class usable in place of a string. Note, however,
> that
> you still have to define a to_s method if you want to change the output
> of
> puts.
>
> I hope this helps
>
> Stefano
0f50b9a2ad85666d537d39bda49327ee?d=identicon&s=25 Jonathan Rochkind (jrochkind)
on 2008-11-18 16:10
Ah, although not completely, when trying to do what I'm trying to do
(basically a proxy class that wraps a string), I wind up getting a stack
overflow error from ruby. Oh well, I'll play with it, might not be
possible (looks like much of String is compiled in C, not in ruby, which
makes it harder for me to reverse engineer/hack), but #to_str was the
right clue to go on.

I wondered why #to_str and #to_s both existed, but my stack overflow
error seems to suggest that in some cases one is defined in terms of the
other. I still wonder why they both exist.

Jonathan

Jonathan Rochkind wrote:
> Thanks, that helps a lot, actually!
>
> Jonathan
>
> Stefano Crocco wrote:
>> Alle Tuesday 18 November 2008, Jonathan Rochkind ha scritto:
>>>
>>> Is there anything I can do so my class will always be converted properly
>>> to a String by ruby, in all cases, so client code can really use it just
>>> like a string?
>>>
>>> Jonathan
>>
>> Defining a to_str method makes String#+ work,  but I'm not sure it'll
>> work
>> everywhere. According to some documentation I found on google, defining
>> that
>> method should make the class usable in place of a string. Note, however,
>> that
>> you still have to define a to_s method if you want to change the output
>> of
>> puts.
>>
>> I hope this helps
>>
>> Stefano
76e6530ed28dc38e4edf93be22e6ccf9?d=identicon&s=25 J. Cooper (nefigah)
on 2008-11-18 22:17
Jonathan Rochkind wrote:
> I wondered why #to_str and #to_s both existed, but my stack overflow
> error seems to suggest that in some cases one is defined in terms of the
> other. I still wonder why they both exist.

The basic rule of thumb is that #to_s is for when we want something to
have a form readable to us, and #to_str is for when we want something to
have a string representation that allows it to be used in place of
actual String arguments.

Thus #print and #puts use #to_s, as they assume we want some readable
output for humans, and most everything else that needs strings for
arguments will try #to_str.

Defining one doesn't define the other.
This topic is locked and can not be replied to.