Ruby on rails 在`+';:字符串可以';不能强制为Fixnum(TypeError)
目前正在处理Ruby中的HackerRank问题。当我试图编译Ruby on rails 在`+';:字符串可以';不能强制为Fixnum(TypeError),ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,目前正在处理Ruby中的HackerRank问题。当我试图编译 in `+': String can't be coerced into Fixnum (TypeError) 下一行 print d + double 我不明白,因为这两个变量都不是字符串 i = 4 d = 4.0 s = 'HackerRank' # Declare second integer, double, and String variables. intOne = 12 double = 4.0 strin
in `+': String can't be coerced into Fixnum (TypeError)
下一行
print d + double
我不明白,因为这两个变量都不是字符串
i = 4
d = 4.0
s = 'HackerRank'
# Declare second integer, double, and String variables.
intOne = 12
double = 4.0
string = "is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!"
# Read and save an integer, double, and String to your variables.
intOne = gets.chomp
double = gets.chomp
string = gets.chomp
# Print the sum of both integer variables on a new line.
print i + intOne
# Print the sum of the double variables on a new line.
print d + double
# Concatenate and print the String variables on a new line
print s + string
# The 's' variable above should be printed first.
如果要将其添加到字符串中,则必须对整型/浮点型调用方法
。\u s
例如:
i = 3
b = ' bah '
c = i.to_s + b
# => '3 bah'
i = '3'
g = i.to_f
# => 3
或者,如果您有这样的字符串:“3”,并且您想要从这个字符串整数中获取,那么您必须调用to_i
方法,如果您想要iteger,to_f
您想要float
例如:
i = 3
b = ' bah '
c = i.to_s + b
# => '3 bah'
i = '3'
g = i.to_f
# => 3
如果要将其添加到字符串中,则必须对整型/浮点型调用方法
。\u s
例如:
i = 3
b = ' bah '
c = i.to_s + b
# => '3 bah'
i = '3'
g = i.to_f
# => 3
或者,如果您有这样的字符串:“3”,并且您想要从这个字符串整数中获取,那么您必须调用to_i
方法,如果您想要iteger,to_f
您想要float
例如:
i = 3
b = ' bah '
c = i.to_s + b
# => '3 bah'
i = '3'
g = i.to_f
# => 3
double
是由于get而产生的字符串。chomp
double
是由于get而产生的字符串。chomp
您已经定义了double
两次:
double = 4.0 #Float type
double = gets.chomp #String type
因此,String
类型的double
已覆盖Float
类型
您已定义:
d = 4.0 #Float type
所以当你这样做的时候:
print d + double #actually you are doing here (Float + String)
您已经定义了两次
double
:
double = 4.0 #Float type
double = gets.chomp #String type
因此,String
类型的double
已覆盖Float
类型
您已定义:
d = 4.0 #Float type
所以当你这样做的时候:
print d + double #actually you are doing here (Float + String)
您还必须调用
。若要在+
之前调用此对象上的,您还必须在+
之前调用此对象上的。若要在+
上面的5行之前调用此对象上的,您需要为其指定一个字符串。当然,这是一个字符串
!在上面的5行中,您为其分配了一个字符串。当然,这是一个字符串
!