# \[Ruby\] Java-like toString() method?

**URL:** <https://rubytalk.org/t/ruby-java-like-tostring-method/5418>\
**Category:** ruby-talk\
**Created:** [29 March 2003 22:47 UTC](https://rubytalk.org/t/ruby-java-like-tostring-method/5418 "2003-03-29T22:47:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan\_Kamper3](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stephan\_Kamper3](https://rubytalk.org/u/Stephan_Kamper3)\
**Post date:** [29 March 2003 22:47 UTC](https://rubytalk.org/t/ruby-java-like-tostring-method/5418/1 "2003-03-29T22:47:57Z")

</div>

Frank Thomas wrote:

> In Java one can define a toString() method which will return a certain  
> string when printing an instance of the object containing the toString()  
> method.
> 
> Is there anything like that in Ruby?

Hi Frank,

I think to\_s is what’s called when you print an object.

HTH

Stephan

---

<div class="post-metadata">

**Author:** ![Martin\_S\_Weber](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Martin\_S\_Weber](https://rubytalk.org/u/Martin_S_Weber)\
**Post date:** [30 March 2003 00:11 UTC](https://rubytalk.org/t/ruby-java-like-tostring-method/5418/2 "2003-03-30T00:11:09Z")

</div>

> Frank Thomas wrote:
> 
> > In Java one can define a toString() method which will return a certain  
> > string when printing an instance of the object containing the toString()  
> > method.
> > 
> > Is there anything like that in Ruby?
> 
> Hi Frank,
> 
> I think to\_s is what’s called when you print an object.

Actually I think it’s inspect, not to\_s - that is depending  
on what the question was _exactly_

> 1  
> 1

> class Fixnum  
> def inspect  
> “Ruby is %d times more fun than Java!” % self  
> end  
> def to\_s  
> “Java is %d times more fun than Ruby!” % self  
> end  
> def lies  
> “#{self}”  
> end  
> end  
> nil

> 1000  
> Ruby is 1000 times more fun than Java!

> 1000.lies  
> “Java is 1000 times more fun than Ruby!”

You see the principle of least surpries applies here, too 🙂

to\_s is being used when you want to interpolate an object into  
a string, as above in the lies method. When you define a to\_s  
method though and wonder why irb keeps ignoring it, well, what  
is being printed when it should put out something’s return value  
is its inspect method.

-Martin 🙂

> **···**
>
> On Sun, Mar 30, 2003 at 07:47:57AM +0900, Stephan Kämper wrote:

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi4](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gabriele\_renzi4](https://rubytalk.org/u/gabriele_renzi4)\
**Post date:** [30 March 2003 14:29 UTC](https://rubytalk.org/t/ruby-java-like-tostring-method/5418/3 "2003-03-30T14:29:37Z")

</div>

I still think to\_s is the right thing.  
Having a string like “bla\n” inspected you get the newline escaped,  
instead of a real new line.

Just, while java println knows how to handle various types, puts or  
print need strings, and I think this is The Right Thing ™

> **···**
>
> il Sun, 30 Mar 2003 10:28:30 +0200, “Frank Thomas” [root@sparkynet.de](mailto:root@sparkynet.de) ha scritto::
> 
> > Test t = new Test();  
> > System.out.print(t); // → “This is a test-class”
> > 
> > ### 
> > 
> > I believe the inspect() method is the equivalent in ruby. (By the way: i
