# Which methods does "puts" invoke?

**URL:** <https://rubytalk.org/t/which-methods-does-puts-invoke/52579>\
**Category:** ruby-talk\
**Created:** [30 March 2009 21:16 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579 "2009-03-30T21:16:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 March 2009 21:16 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/1 "2009-03-30T21:16:14Z")

</div>

Hi, if I redefine "to\_s" or "inspect" methods in String class I still get the  
original output when doing:

&nbsp;&nbsp;puts string

Which String@method is invoked when executing "puts"?

Thanks.

> **···**
>
> --  
> Iñaki Baz Castillo \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Eric\_Jacoboni2](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@Eric\_Jacoboni2](https://rubytalk.org/u/Eric_Jacoboni2)\
**Post date:** [30 March 2009 21:34 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/2 "2009-03-30T21:34:54Z")

</div>

Iñaki Baz Castillo \<ibc@aliax.net\> writes:

> Hi, if I redefine "to\_s" or "inspect" methods in String class I still get the  
> original output when doing:
> 
> &nbsp;&nbsp;puts string
> 
> Which String@method is invoked when executing "puts"?

to\_s (inspect is called by p)

> > class Truc  
> > end

=\> nil

> > machin = Truc.new

=\> #\<Truc:0x55604\>

> > puts machin

#\<Truc:0x55604\>  
=\> nil

> > class Truc  
> > &nbsp;&nbsp;def to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;"to\_s called"  
> > &nbsp;&nbsp;end  
> > end

=\> nil

> > puts machin

to\_s called  
=\> nil

---

<div class="post-metadata">

**Author:** ![Lyle\_Johnson5](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@Lyle\_Johnson5](https://rubytalk.org/u/Lyle_Johnson5)\
**Post date:** [30 March 2009 21:44 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/3 "2009-03-30T21:44:49Z")

</div>

Based on my cursory inspection of the internals (in io.c) it doesn't  
appear that puts() is actually calling any of String's methods, at  
least not in any way that you can override them.

> **···**
>
> On Mon, Mar 30, 2009 at 4:16 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > Hi, if I redefine "to\_s" or "inspect" methods in String class I still get the  
> > original output when doing:
> > 
> > puts string
> > 
> > Which String@method is invoked when executing "puts"?

---

<div class="post-metadata">

**Author:** ![Rob\_Olson](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@Rob\_Olson](https://rubytalk.org/u/Rob_Olson)\
**Post date:** [30 March 2009 23:34 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/4 "2009-03-30T23:34:52Z")

</div>

From the rdoc of IO#puts ([http://www.ruby-doc.org/core-1.8.7/classes/](http://www.ruby-doc.org/core-1.8.7/classes/)  
IO.html#M000474) it states that puts "writes the given objects to ios  
as with IO#print". So puts calls print.

Further, the rdoc for IO#print ssays that objects passed to it "that  
aren‘t strings will be converted by calling their to\_s method."

> **···**
>
> On Mar 30, 2:16 pm, Iñaki Baz Castillo \<i...@aliax.net\> wrote:
> 
> > Hi, if I redefine "to\_s" or "inspect" methods in String class I still get the  
> > original output when doing:
> > 
> > puts string
> > 
> > Which String@method is invoked when executing "puts"?
> > 
> > Thanks.
> > 
> > --  
> > Iñaki Baz Castillo \<i...@aliax.net\>
> 
> --  
> Rob Olson  
> [http://thinkingdigitally.com](http://thinkingdigitally.com)

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [30 March 2009 21:53 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/5 "2009-03-30T21:53:35Z")

</div>

Well, I already tested it, and yes, it seems to work if you define to\_s method  
in some custom class, but note the following:

> **···**
>
> El Lunes 30 Marzo 2009, Eric Jacoboni escribió:
> 
> > Iñaki Baz Castillo \<ibc@aliax.net\> writes:  
> > \> Hi, if I redefine "to\_s" or "inspect" methods in String class I still get  
> > \> the original output when doing:  
> > \>  
> > \> puts string  
> > \>  
> > \> Which String@method is invoked when executing "puts"?
> > 
> > to\_s (inspect is called by p)
> > 
> > \>\> class Truc  
> > \>\> end
> > 
> > =\> nil
> > 
> > \>\> machin = Truc.new
> > 
> > =\> #\<Truc:0x55604\>
> > 
> > \>\> puts machin
> > 
> > #\<Truc:0x55604\>  
> > =\> nil
> > 
> > \>\> class Truc  
> > \>\> def to\_s  
> > \>\> "to\_s called"  
> > \>\> end  
> > \>\> end
> > 
> > =\> nil
> > 
> > \>\> puts machin
> > 
> > to\_s called  
> > =\> nil
> 
> --------  
> class String  
> &nbsp;&nbsp;def to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;"hiiiiiii"  
> &nbsp;&nbsp;end  
> end
> 
> # s=String.new("qweqwe")  
> # puts s  
> qweqwe  
> nil  
> -----------
> 
> ¿?
> 
> --  
> Iñaki Baz Castillo \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Eric\_Jacoboni2](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@Eric\_Jacoboni2](https://rubytalk.org/u/Eric_Jacoboni2)\
**Post date:** [30 March 2009 22:34 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/6 "2009-03-30T22:34:59Z")

</div>

Iñaki Baz Castillo \<ibc@aliax.net\> writes:

> --------  
> class String  
> &nbsp;&nbsp;def to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;"hiiiiiii"  
> &nbsp;&nbsp;end  
> end
> 
> # s=String.new("qweqwe")  
> # puts s  
> qweqwe  
> nil  
> -----------

Oh yes, you're right... puts doesn't use to\_s when called on a String.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [31 March 2009 09:02 UTC](https://rubytalk.org/t/which-methods-does-puts-invoke/52579/7 "2009-03-31T09:02:59Z")

</div>

Eric Jacoboni wrote:

> Oh yes, you're right... puts doesn't use to\_s when called on a String.

Nor on nil. (It prints "nil", whereas nil.to\_s is the empty string)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
