我從一個哈希檢索信息快速的問題,這裏是代碼至今:檢索從哈希信息散列內 - 紅寶石
permits_sheet.each do |row|
rate_type = row[0].to_s #Converts the rate type (title) to a string
row.shift #Removes the title from hash so it's not added to values
row.each do |values|
split_value = values.split ('=') #References relations from an excel sheet pulled in earlier. EG: T=2324, W=8633
(@@permits_hash[rate_type] ||= []) << {split_value[0].to_s => split_value[1]} #Multiple values exist within each title
end
end
puts @@permits_hash['R']['T'] #In this example I'm searching for the hash key of T under the R title. I expected it to return the 2324 from the example above.
當試圖以這種方式它會導致一個錯誤檢索信息。我確信我只是做了一些愚蠢的事情,但是任何幫助都將不勝感激(在相當一段時間裏還沒有使用過Ruby)。
感謝您的幫助!
這將是有益的,如果你能發佈的錯誤信息。 – 2010-09-21 19:32:09
Test.rb:117:in'[]':無法將字符串轉換爲整數(TypeError) 所有內容都已轉換爲字符串,所以我遇到了一些排查問題的問題。 – 2010-09-21 19:34:30
您正在使用字符串爲數組建立索引。 – yxhuvud 2010-09-21 19:41:26