Ruby-on-rails 类型错误:没有将符号隐式转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21402111/
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
TypeError: no implicit conversion of Symbol into Integer
提问by Severin
I encounter a strange problem when trying to alter values from a Hash. I have the following setup:
尝试更改哈希值时遇到一个奇怪的问题。我有以下设置:
myHash = {
company_name:"MyCompany",
street:"Mainstreet",
postcode:"1234",
city:"MyCity",
free_seats:"3"
}
def cleanup string
string.titleize
end
def format
output = Hash.new
myHash.each do |item|
item[:company_name] = cleanup(item[:company_name])
item[:street] = cleanup(item[:street])
output << item
end
end
When I execute this code I get: "TypeError: no implicit conversion of Symbol into Integer" although the output of item[:company_name] is the expected string. What am I doing wrong?
当我执行此代码时,我得到:“TypeError:没有将符号隐式转换为整数”,尽管 item[:company_name] 的输出是预期的字符串。我究竟做错了什么?
采纳答案by Marek Lipka
Your itemvariable holds Arrayinstance (in [hash_key, hash_value]format), so it doesn't expect Symbolin []method.
您的item变量包含Array实例([hash_key, hash_value]格式),因此它不期望Symbol在[]方法中。
This is how you could do it using Hash#each:
这是您可以使用的方法Hash#each:
def format(hash)
output = Hash.new
hash.each do |key, value|
output[key] = cleanup(value)
end
output
end
or, without this:
或者,没有这个:
def format(hash)
output = hash.dup
output[:company_name] = cleanup(output[:company_name])
output[:street] = cleanup(output[:street])
output
end
回答by BroiSatse
This error shows up when you are treating an array or string as a Hash. In this line myHash.each do |item|you are assigning itemto a two-element array [key, value], so item[:symbol]throws an error.
当您将数组或字符串视为哈希时,会出现此错误。在这一行中,myHash.each do |item|您将分配item给一个二元素数组[key, value],因此item[:symbol]会引发错误。
回答by Sergio Tulentsev
You probably meant this:
你可能是这个意思:
require 'active_support/core_ext' # for titleize
myHash = {company_name:"MyCompany", street:"Mainstreet", postcode:"1234", city:"MyCity", free_seats:"3"}
def cleanup string
string.titleize
end
def format(hash)
output = {}
output[:company_name] = cleanup(hash[:company_name])
output[:street] = cleanup(hash[:street])
output
end
format(myHash) # => {:company_name=>"My Company", :street=>"Mainstreet"}
Please read documentation on Hash#each
请阅读有关Hash#each 的文档
回答by Alok Anand
myHash.each{|item|..}is returning you array object for itemiterative variable like the following :--
myHash.each{|item|..}正在为item迭代变量返回数组对象,如下所示:--
[:company_name, "MyCompany"]
[:street, "Mainstreet"]
[:postcode, "1234"]
[:city, "MyCity"]
[:free_seats, "3"]
You should do this:--
你应该做这个: -
def format
output = Hash.new
myHash.each do |k, v|
output[k] = cleanup(v)
end
output
end
回答by Mikey_Gnote
Ive come across this many times in my work, an easy work around that I found is to ask if the array element is a Hash by class.
我在工作中多次遇到过这种情况,我发现一个简单的解决方法是询问数组元素是否为类的 Hash。
if i.class == Hash
notation like i[:label] will work in this block and not throw that error
end

