On Sun, 18 Feb 2001, Stephen White wrote:

> On Sun, 18 Feb 2001, Brent Rowland wrote:
> 
> > s = "and the answer is: "
> > list.each {|item| s << item }
> > 
> > testltlt.rb:4:in `<<': failed to convert Float into String (TypeError)
> 
> For the same reason you can't do:
> 
>   a = "hello"
>   a << 1.23
> 
>   irb(main):011:0> a << 1.23
>   TypeError: failed to convert Float into String
> 
> The reason it works with the first element:
> 
>   a = "hello"
>   a << 1
> 
>   irb(main):003:0> a << 1
>   "hello\001"
> 
> Ruby is treating the "1" as a character value, not as a number.


I notice, though, that the pickaxe book gives the argument type for
String#<< as aFixnum or anObject (not aString), and in string.c there
does seem to be some conversion going on:

  VALUE
  rb_str_append(str1, str2)
      VALUE str1, str2;
  {
      if (TYPE(str2) != T_STRING) str2 = rb_str_to_str(str2);
  ...

(rb_str_append is called by rb_str_concat)

And yet, this can't be the whole story, since the TypeErrors do
happen.  Further detective work (and/or clarification by those ahead
of me in the process :-) required....


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav