【问题标题】:Why am I getting NoMethodError Iterating on Hash Embedded in Array in Ruby?为什么我在 Ruby 的数组中嵌入哈希时得到 NoMethodError 迭代?
【发布时间】:2018-10-07 09:18:09
【问题描述】:

我不断收到我的代码的以下错误消息。 (尝试遍历嵌入在数组中的哈希以返回最大值及其关联的键)。 错误出现在第一个循环之后。

Traceback(最近一次调用最后):6:来自 main.rb:53:in <main>' 5: from main.rb:53:inmap' 4:来自 main.rb:53:in each' 3: from main.rb:53:intimes' 2:来自 main.rb:58:在block in <main>' 1: from main.rb:58:ineach' main.rb:60:in block (2 levels) in <main>': undefined method[]' for nil:NilClass (NoMethodError)

#assigns number of participants in array 
num_participants = 2 

#embedded hash within array (of participants)

participants = [{"participant_name"=> "Liz Lee", "cupcakes_sold" => 41, "cupcakes_left" => 31, "cakes_sold" => 12, "cakes_left" => 2}, 
{"participant_name" => "John Jay", "cupcakes_sold" => 44, "cupcakes_left" => 2, "cakes_sold" => 22, "cakes_left" => 4}] 

#calculates total amount raised by each hash in participants array
#and populates new hash key 'proceeds' for each hash in participants array
#then loops through to update most_raised amount and assign highest_earner

counter = 0
most_raised = 0
highest_earner = ""

num_participants.times.map do 

 profits =  2 * participants[0 + counter]['cookies_sold']  - 
 participants[0 + counter]['cookies_left'] - 
 participants[0 + counter]['cookies_sold'] + 
 6 * participants[0 + counter]['cakes_sold'] - 
 3 * participants[0 + counter]['cakes_sold'] - 
 3 * participants[0 + counter]['cakes_left']
 participants[0 + counter]['proceeds'] = profits.to_i
 puts "\nProceeds raised by #{participants[0+counter]['participant_name'].capitalize}: 
 $#{participants[0+counter]['proceeds']}" +"."
 counter+=1

 ['participant_name', 'proceeds'].each do 

  if (max < profits)
   most_raised = participants[0+counter]['proceeds'].to_i
   highest_earner = participants[0+counter]['participant_name']
  end
 end
end

puts "#{highest_earner.capitalize} raised the most:$#{most_raised}"

【问题讨论】:

  • participants 是什么,你不是忘记分配块变量了吗?
  • 参与者是我的哈希数组
  • 在任何地方都看不到它
  • 如果participants 确实存在,则错误可能意味着0+counter 超出其范围。
  • 你在哪里设置num_participants?

标签: ruby parameters hashmap nomethoderror aor-embedded-array


【解决方案1】:

解决方案:我分离了我的循环。

#calculates total amount raised by each participant
counter = 0
participants.each do 
  profits =  2 * participants[0 + counter]['cookies_sold']  - 
             participants[0 + counter]['cookies_left'] - participants[0 + counter]['cookies_sold'] + 6 * participants[0 + counter]['cakes_sold'] - 
             3 * participants[0 + counter]['cakes_sold'] - 3 * 
             participants[0 + counter]['cakes_left']
  participants[0 + counter]['proceeds'] = profits.to_i
  puts "\n\e[36mProceeds raised by #{participants[0+counter]['participant_name'].capitalize}:\e[0m $#{participants[0+counter]['proceeds']}" +"."
  counter+=1
end

#calculates highest earner and highest amount raised
counter = 0
most_raised = 0 
highest_earner = ""
participants.each do 
  if (most_raised < participants[0+counter]['proceeds'])
    most_raised = participants[0+counter]['proceeds'].to_i
    highest_earner = participants[0+counter]['participant_name']
    counter+=1
  end
end
puts most_raised 
puts highest_earner

【讨论】:

    【解决方案2】:

    由于您设置了 counter=0,因此您在执行参与者[0 + counter] 时永远不会超过第一个哈希(始终等于参与者[0])

    我错过了你的柜台+=1 抱歉,这个答案无关紧要。

    【讨论】:

    • 所以,循环用于更新我在数组中的位置(bc 第一个循环正在工作并且它填充了“proceeds”键).. 问题出在第二个循环的语法中
    • 我做了单独的循环,并且不知何故奏效了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2015-08-30
    • 2012-05-19
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多