# Let my custom defined class convert to string?

**URL:** <https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501>\
**Category:** Ruby\
**Created:** [November 18, 2008, 3:04am UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501 "2008-11-18T03:04:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 3:04am UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/1 "2008-11-18T03:04:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 3:37am UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/2 "2008-11-18T03:37:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 8:46am UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/3 "2008-11-18T08:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/4 "2008-11-18T16:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/5 "2008-11-18T16:10:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [November 18, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/let-my-custom-defined-class-convert-to-string/153501/6 "2008-11-18T22:17:59Z")

</div>

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.
