From: Joel VanderWerf Date: 2010-08-05T05:16:36+09:00 Subject: Re: A little problem Amir Ebrahimifard wrote: > Hi > What is difference between these 2 codes ? > (when I executed these codes result was the same ) > > --------------------- > def welcome1( name ) > puts "Hello#{name}" > end > > def welcome2( name ) > puts "Hello"+name > end > > welcome1( Amir ) > welcome2( Amir ) > > --------------------- The first calls #to_s and the second calls #to_str. The #to_str method is usually only defined for objects that are already String-like. The #to_s is usually defined for everything. def welcome1( name ) puts "Hello#{name}" end def welcome2( name ) puts "Hello"+name end Amir = Object.new def Amir.to_s puts "to_s" "Amir" end def Amir.to_str puts "to_str" "Amir" end welcome1( Amir ) welcome2( Amir )