我正在尝试使用字符串的模块化函数%来获取哈希,并将其值注入到字符串的适当位置,但我总是接收key{x} not found (KeyError),尽管我可以确认键在那里。我做错了什么?
s = "Invalid: %{totalInvalid} , OutofThreshold: %{totalOutOfThreshold} "
puts row.fetch ('totalInvalid') #<-Just checking to make sure the key is in there
ext = s % row我得到了这个输出:
0 #<- Key does seem to be in there, returns correct value
in `%': key{totalInvalid} not found (KeyError)散列是从微型tds (命中SQL服务器)提供的,当在其上使用puts时:
{"environment"=>"prd ", "locale"=>"uk ", "totalProducts"=>666, "to
talOutOfThreshold"=>0, "totalInvalid"=>0, "epochtime"=>1444444444, "thresholdPro
ductIds"=>"", "invalidProductIds"=>""}发布于 2015-03-10 13:43:25
在这里,散列键应该是符号而不是字符串,因此尝试以下操作:
to_inject = row.each_with_object({}) { |(key, value), h| h[key.to_sym] = value }
s = "Invalid: %{totalInvalid} , OutofThreshold: %{totalOutOfThreshold} "
ext = s % to_inject这会有帮助的!
https://stackoverflow.com/questions/28964629
复制相似问题