From: "Hal E. Fulton" Date: 2000-11-30T21:50:53+09:00 Subject: [ruby-talk:6672] Re: printing/accessing arrays and hashes ----- Original Message ----- From: Dave Thomas To: ruby-talk ML Sent: Wednesday, November 29, 2000 9:41 PM Subject: [ruby-talk:6665] Re: printing/accessing arrays and hashes > raja@cs.indiana.edu (Raja S.) writes: > > > Interesting that Ruby does a #to_s on the elements of the array > > first. > > Actually, what happens is that when you concatenate something to an > existing string, if that something isn't already a string, its to_str > method is called. > > "a" + 1 > -:1:in `+': failed to convert Fixnum into String (TypeError) > from -:1 > > class Fixnum > def to_str > "Number: " + to_s > end > end > > "a" + 1 #=> "aNumber: 1" > That's interesting, Dave. Is the behavior of << the same? Because I have found that the docs say "anObject" but it only seems to work with another string. Why, incidentally, is to_str called instead of to_s? I have never understood that yet. Same for to_a and to_ary. Hal