Array TypeError: can't convert Fixnum into String

后端 未结 6 1845
半阙折子戏
半阙折子戏 2021-01-01 10:04

I am experimenting with arrays, and am reading the book \"Beginning Ruby on Rails\" by Steve Holzner. I made the program:

array = [\'Hello\', \'there\', 1, 2         


        
6条回答
  •  暖寄归人
    2021-01-01 11:07

    you are trying to add an integer(fixnum) and a string, which is something you can't do on ruby unless you explicitly cast the integer(fixnum) to a string. in your code array2[0]holds a string value "banannas" and array2[1] holds an integer(fixnum) value 1. so for your code too run correctly you need too cast the value in array2[1] to a string value.

    you can change your code on line 9 to this:

    puts array2[0] + " " + array2[1]._s
    

提交回复
热议问题