ruby – ”不能将Fixnum转换为String(TypeError)

栏目: Ruby · 发布时间: 6年前

内容简介:http://stackoverflow.com/questions/14895594/cant-convert-fixnum-into-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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Text Processing in Python

Text Processing in Python

David Mertz / Addison-Wesley Professional / 2003-6-12 / USD 54.99

Text Processing in Python describes techniques for manipulation of text using the Python programming language. At the broadest level, text processing is simply taking textual information and doing som......一起来看看 《Text Processing in Python》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具