我打了一个’不能将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技能.
解决方法@H_301_8@
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!"
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!"