Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在`+';:字符串可以';不能强制为Fixnum(TypeError)_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 在`+';:字符串可以';不能强制为Fixnum(TypeError)

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

目前正在处理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
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行中,您为其分配了一个
字符串。当然,这是一个
字符串