Let my custom defined class convert to string?


#1

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


#2

On 18.11.2008, at 02:01 , Jonathan R. wrote:

complains that it can’t convert MyClass to a string.

class MyClass < String

end

Einar Magnús Boson
+354-661 1649
removed_email_address@domain.invalid
removed_email_address@domain.invalid


#3

Alle Tuesday 18 November 2008, Jonathan R. 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


#4

Thanks, that helps a lot, actually!

Jonathan

Stefano C. wrote:

Alle Tuesday 18 November 2008, Jonathan R. 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


#5

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 R. wrote:

Thanks, that helps a lot, actually!

Jonathan

Stefano C. wrote:

Alle Tuesday 18 November 2008, Jonathan R. 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


#6

Jonathan R. 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.