[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Which methods does "puts" invoke?

Iñaki Baz Castillo

3/30/2009 9:16:00 PM

Hi, if I redefine "to_s" or "inspect" methods in String class I still get t=
he=20
original output when doing:

puts string

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

Thanks.

=2D-=20
I=C3=B1aki Baz Castillo <ibc@aliax.net>

6 Answers

Eric Jacoboni

3/30/2009 9:30:00 PM

0

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

Lyle Johnson

3/30/2009 9:45:00 PM

0

On Mon, Mar 30, 2009 at 4:16 PM, I=F1aki 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:
>
> =A0puts string
>
> Which String@method is invoked when executing "puts"?

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.

Iñaki Baz Castillo

3/30/2009 9:54:00 PM

0

El Lunes 30 Marzo 2009, Eric Jacoboni escribi=F3:
> I=F1aki Baz Castillo <ibc@aliax.net> writes:
> > Hi, if I redefine "to_s" or "inspect" methods in String class I still g=
et
> > 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
>
> =3D> nil
>
> >> machin =3D Truc.new
>
> =3D> #<Truc:0x55604>
>
> >> puts machin
>
> #<Truc:0x55604>
> =3D> nil
>
> >> class Truc
> >> def to_s
> >> "to_s called"
> >> end
> >> end
>
> =3D> nil
>
> >> puts machin
>
> to_s called
> =3D> nil


Well, I already tested it, and yes, it seems to work if you define to_s met=
hod=20
in some custom class, but note the following:

=2D-------
class String
def to_s
"hiiiiiii"
end
end

# s=3DString.new("qweqwe")
# puts s
qweqwe
nil
=2D----------

=BF?

=2D-=20
I=F1aki Baz Castillo <ibc@aliax.net>

Eric Jacoboni

3/30/2009 10:32:00 PM

0

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


> --------
> class String
> def to_s
> "hiiiiiii"
> 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.

Rob Olson

3/30/2009 11:32:00 PM

0

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>


From the rdoc of IO#puts (http://www.ruby-doc.org/core-1.8....
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."

--
Rob Olson
http://thinkingdig...

Brian Candler

3/31/2009 9:03:00 AM

0

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