"Hal E. Fulton" <hal9000 / hypermetrics.com> writes:

> 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.

It works the same way.

   class Dave
   end

   d = Dave.new

   "a" << d

   -:6:in `<<': failed to convert Dave into String (TypeError)
	from -:6


   class Dave
      def to_str
        "ouch"
      end
   end

   d = Dave.new

   "a" << d    #=> "aouch"


> Why, incidentally, is to_str called instead of to_s? I have never
> understood that yet. Same for to_a and to_ary.

My understanding is that to_s says "convert yourself to some string
representation". You can call to_s on any object, and will get some
kind of response. to_str however is used when you're expecting the
received to be string-like already.


Dave