誰能告訴我爲什麼我在下面的代碼中收到此錯誤?錯誤:沒有將字符串隱式轉換爲整數
def total_single_order(one_order)
single_order_totals = Hash.new(0)
one_order.each do |coffee_sku, coffee_info_hash|
binding.pry
single_order_totals[coffee_sku]['cost_to_customer'] = (coffee_info_hash["RetailPrice"].to_f * coffee_info_hash['num_bags]'].to_f)
single_order_totals[coffee_sku]['cost_to_company'] = (coffee_info_hash["PurchasingPrice"].to_f * coffee_info_hash['num_bags]'].to_f)
end
single_order_totals
end
total_single_order(one_order)
你正確抄寫的代碼?如圖所示,該代碼應生成一個錯誤'未定義方法[]爲上線nil'指派到'single_order_totals [coffee_sku] [「cost_to_customer」]'?請顯示確切的錯誤,標識堆棧跟蹤頂部的行。 –
謝謝彼得。爲了得到這個工作,我使用了錯誤修正後的初始化方式,如下所示:single_order_totals = Hash.new {| hash,key | hash [key] = []} – John