From: "David A. Black" Date: 2004-09-17T11:25:50+09:00 Subject: Re: Ex-Perl coders: Howz it feel to convert to Ruby? Hi -- On Fri, 17 Sep 2004, Brian Wisti wrote: > > --- "David A. Black" wrote: > > > Hi -- > > > > On Fri, 17 Sep 2004, Brian Wisti wrote: > > > > > > > > --- Nate Smith wrote: > > > > > > > Try > > > > > > > > print "a" + 1 > > > > > > > > Won't work -- you have to do > > > > > > > > print "a" + 1.to_s > > > > > > > > > > > > Nate > > > > > > That "+" means you're building a new string from two Strings. Since > > the > > > second operand is not a String, the conversion has to be explicit. > > > > If the second operand responds to to_str, then its response to to_str > > will be used in cases where only a string will do: > > > > irb(main):016:0> obj = Object.new > > => # > > irb(main):017:0> puts "this is an " + obj > > TypeError: cannot convert Object into String > > from (irb):17:in `+' > > from (irb):17 > > from :0 > > irb(main):018:0> def obj.to_str; "implicit string representation!"; > > end > > => nil > > irb(main):019:0> puts "this is an " + obj > > this is an implicit string representation! > > So we just have Fixnums and similar types not responding to to_s? > Interesting... No, they do respond to to_s, but not to to_str. It's to_str that provides this kind of masquerading facility. David -- David A. Black dblack@wobblini.net