Iñaki Baz Castillo
3/30/2009 9:54:00 PM
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>