2013-11-22 2 views
-1

아래 코드에서이 오류가 발생하는 이유는 누구나 알 수 있습니까?오류 : 문자열을 정수로 변환하지 않음

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) 
+0

당신은 정확하게 코드를 쓰다나요? 이 코드는'single_order_totals [coffee_sku] [ 'cost_to_customer']'에 할당 된 줄에''undefined method [] for nil' 오류를 생성해야합니까? 스택 추적 상단에 줄을 식별하는 정확한 오류를 표시하십시오. –

+0

감사합니다. 이것이 오작동을 일으키기 위해 오타 오류 수정을 한 후에 나는 이렇게 초기화해야했습니다 : single_order_totals = Hash.new {| hash, key | hash [key] = []} – John

답변

1

위의 주석에서 언급 한 문제 외에도 5 행과 6 행에 오타가 있습니다. 또한

coffee_info_hash['num_bags'] 

해야처럼

coffee_info_hash['num_bags]'] 

single_order_totalsHash.new(0)에서 0의 기본 값으로 초기화되기 때문에 single_order_totals[coffee_sku]가 제로로 평가 보인다.

당신이지고있어 오류가

coffee_info_hash["RetailPrice"] 

아마도 모든 SKU를 정수는

에서 오는 것 같다. 그래서 여러분은 여기 실제 정수에 'RetailPrice'에 접근하려고합니다.

것과 유사한 예 :

sku = 45 
sku["key"] 
#=> in `[]': no implicit conversion of String into Integer (TypeError) 
+1

감사합니다. @mmromer. 나는 타입을 변경했고 싱글을 초기화 할 필요가 있다는 것을 발견했다. single_order_totals = Hash.new {| hash, key | hash [key] = []} 나머지 작업을 수행하려면 – John

관련 문제