From: itsme213@... (you CAN teach an old dog ...) Date: 2003-06-13T15:50:31+09:00 Subject: Re: Confused about to_s in Ruby / irb "Hal E. Fulton" wrote > I guess you know about p and puts, right? > puts obj > will call obj.to_s, but > p obj > will call obj.inspect instead. Sounds reasonable, but it does not explain the behavior below. It seems (p x) calls x.inspect if it is defined; otherwise it calls x.to_s >class None; end ==> nil >p None.new ==> # >puts None.new ==> # >class To_S; def to_s; "to_s "; end; end ==> nil >p To_S.new # how come? ==> to_s >puts To_S.new ==> to_s >class Inspect; def inspect; "inspect "; end; end ==> nil >p Inspect.new ==> inspect >puts Inspect.new ==> # >class Both > def to_s; "Both: to_s "; end > def inspect; "Both: inspect "; end >end ==>nil >p Both.new ==> Both: inspect >puts Both.new ==> Both: to_s