ruby – ”不能將Fixnum轉換為String(TypeError)
我打了一個’不能將Fixnum轉換成String(TypeError)’錯誤,而它似乎很簡單,我不知道如何解決它.我認為我的邏輯是聲音 – 將輸入的字串變數轉換為整數,然後執行基本操作 – 但顯然我缺少一些關鍵的資訊.
puts 'What is your favourite number?' favenum = gets.chomp better = favenum.to_i + 1 puts 'Yeah '+favenum+' is nice enough but '+better+' is bigger and better by far! Think on.'
已經嘗試尋找一個答案,但同樣的錯誤的例子現在有超出我的基本的紅寶石技能.
Ruby(與其他一些語言不同)在String#方法中的運算元時不會將物件轉換為字串.手動轉換為字串:
puts 'Yeah ' + favenum.to_s + ' is nice enough but ' + better.to_s + ' is bigger and better by far!'
或使用字串插值(注意雙引號):
puts "Yeah #{favenum} is nice enough but #{better} is bigger and better by far!"
http://stackoverflow.com/questions/14895594/cant-convert-fixnum-into-string-typeerror