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. This can be
verified by this:

  a = "hello"
  a << 4237432

  irb(main):013:0> a << 4237432
  TypeError: failed to convert Fixnum into String

Perl will automatically convert between types. Ruby won't make these
assumptions on your behalf, which is a little annoying in the short
term but a choice that I like in the long term.

You can achieve what you want with:

  list.each {|item| s << item.to_s }

Which explicitly converts each element to a string. This will give you:

  "and the answer is: 12.3fourfalse"

You can also do:

  s = "and the answer is: " + list.join(', ')

which gives:

  "and the answer is: 1, 2.3, four, false"

-- 
  spwhite / chariot.net.au