ruby 使用 Modulo 找不到密钥

ruby key not found using Modulo

我正在尝试使用字符串的取模函数 % 来获取散列并将其值注入字符串内的适当位置,但我总是收到 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"=>""}

在这里,哈希键应该是符号而不是字符串,所以请尝试以下操作:

to_inject = row.each_with_object({}) { |(key, value), h| h[key.to_sym] = value  }
s = "Invalid: %{totalInvalid} , OutofThreshold: %{totalOutOfThreshold} "
ext = s % to_inject

这应该有所帮助!