ruby 数组类型错误:无法将 Fixnum 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9677945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:53:17  来源:igfitidea点击:

Array TypeError: can't convert Fixnum into String

rubyarrays

提问by Billjk

I am experimenting with arrays, and am reading the book "Beginning Ruby on Rails" by Steve Holzner. I made the program:

我正在试验数组,并且正在阅读 Steve Holzner 所著的“Beginning Ruby on Rails”一书。我做了这个程序:

array = ['Hello', 'there', 1, 2]
puts array[1]
puts array[3]
puts array.length
array2 = Array.new
puts array2.length
array2[0] = "Banana"
array2[1] = 6
puts array2[0] + " " + array2[1]
puts array3.length

It doesn't do much, but when i run it i get the error

它没有做太多,但是当我运行它时,我收到错误

arrays.rb:9:in `+': can't convert Fixnum into String (TypeError)
    from arrays.rb:9

Why do I get this error?

为什么我会收到这个错误?

回答by JP Silvashy

You can't add a string and a integer (Fixnum), in this case you tried to add 6 to "Banana".

您不能添加字符串和整数 ( Fixnum),在这种情况下,您尝试将 6 添加到“香蕉”。

If on line 9 you did this:

如果在第 9 行你这样做了:

puts array2[0] + " " + array2[1].to_s

You would get:

你会得到:

"Banana 6"

回答by Marc Talbot

array2[1]is 6, which is a Fixnum. It doesn't know how to add itself to a string (which in this case is Banana. If you were to convert it to a string, it would work just fine.

array2[1]is 6,这是一个 Fixnum。它不知道如何将自己添加到一个字符串中(在这种情况下是Banana. 如果您要将其转换为字符串,它会工作得很好。

puts array2[0] + " " + array2[1].to_s

回答by Kevin Jalbert

The error is basically saying you cannot convert array2[1](the value is a number, a type Fixnumin this case) into a Stringtype. The way you would work around this is to convert the type into a String(this is for line 9 where the error occurs):

该错误基本上是说您无法将array2[1](值是一个数字,在本例中为Fixnum类型)转换为String类型。解决此问题的方法是将类型转换为字符串(这是针对发生错误的第 9 行):

puts array2[0] + " " + array2[1].to_s

The array2[1].to_sconverts the number into a String type.

数组2[1]。to_s将数字转换为 String 类型。

回答by Chetter Hummin

Haven't tried this myself, but try replacing

自己没试过,但尝试更换

puts array2[0] + " " + array2[1]

with

puts array2[0] + " " + array2[1].to_s

回答by Aaron

Here is a way to convert a FixNum expression into a string,

这是一种将 FixNum 表达式转换为字符串的方法,

x=2
print (x+20).to_s + "\sbanannas"

Didn't know you could tack on the FixnNum#to_s method to those parens.

不知道您可以将 FixnNum#to_s 方法添加到这些括号中。

回答by Yoseph.B

you are trying to add an integer(fixnum) and a string, which is something you can't do on ruby unless you explicitly cast the integer(fixnum) to a string. in your code array2[0]holds a string value "banannas" and array2[1]holds an integer(fixnum) value 1. so for your code too run correctly you need too cast the value in array2[1] to a string value.

您正在尝试添加一个 integer(fixnum) 和一个字符串,除非您明确将 integer(fixnum) 转换为字符串,否则您无法在 ruby​​ 上执行此操作。在您的代码中array2[0]保存一个字符串值“banannas”并array2[1]保存一个整数(fixnum)值 1。所以为了您的代码也能正确运行,您也需要将 array2[1] 中的值转换为字符串值。

you can change your code on line 9 to this:

您可以将第 9 行的代码更改为:

puts array2[0] + " " + array2[1]._s