如何将哈希嵌入到数组中并在Ruby中迭代这些哈希?

时间:2018-04-22 02:06:44

标签: arrays ruby hash

我想在数组中创建几个哈希并遍历该数组以填充不同的键值。

participant = {
"participant_name" => "",
"cupcakes_sold" => 0, 
"cakes_sold" => 0,
"cupcakes_left" => 0,
"cakes_left" => 0
}

participants = Array.new(4)

#loops through 4 times to populate hash with values  
participants.each do |participant| 
    puts "\nPlease enter participant name:  "
    participant["participant_name"] = gets.chomp
    puts "\nHow many cupcakes did " + participant.fetch("participant_name") + " sell?"
    participant["cupcakes_sold"] = gets.chomp.to_i
    puts "\nHow many does " + participant.fetch("participant_name") + " have left?"
    participant["cupcakes_left"] = gets.chomp.to_i
    puts "\nHow many cakes did " + participant.fetch("participant_name") + " sell?"
    participant["cakes_sold"] = gets.chomp.to_i
    puts "\nHow many does " +  participant.fetch("participant_name") + " have left?"
    participant["cakes_left"] = gets.chomp.to_i
    participants << participant
end

puts participants

因此,我不确定如何使用哈希值填充数组。我也不确定如何遍历数组中的嵌入式哈希。我也得到参与者的错误=新的数组(4)。我是否正确地实例化了?

我认为要填充数组,我可以添加参与者&lt;&lt;我的循环参与者?

2 个答案:

答案 0 :(得分:0)

这是实现这一目标的一种方法。

4.times.map do
  participant = {} 
  puts "\nPlease enter participant name"
  name = gets.chomp
  participant["participant_name"] = name
  ["cupcakes", "cakes"].each do |item|
    puts "How many #{item} did #{name} sell?"  
    participant["#{item}_sold"] = gets.to_i
    puts "How many does #{name} have left?"  
    participant["#{item}_left"] = gets.to_i
  end
  participant
end
  #=> [{ "participant_name"=>"Billy-Bob", "cupcakes_sold"=>46, "cupcakes_left=>12",
  #      "cakes_sold=>12, "cakes_left=>2 },
  #    { "participant_name"=>"Trixy", "cupcakes_sold"=>112, "cupcakes_left=>0",
  #      "cakes_sold=>85, "cakes_left=>0 },
  #    { "participant_name"=>"Bub", "cupcakes_sold"=>2, "cupcakes_left=>104",
  #      "cakes_sold=>1, "cakes_left=>18 },
  #    { "participant_name"=>"Wilma", "cupcakes_sold"=>64, "cupcakes_left=>"31",
  #      "cakes_sold=>18, "cakes_left=>9 }]

请注意,chomp中不需要"3\n".chomp.to_i #=> 3"3\n".to_i #=> 3

答案 1 :(得分:0)

我认为一个问题是,当我启动大小为4的数组时,我创建了一个包含四个nil元素的数组。当它在我的数组上调用inspect(participant.inspect)时,我看到前四个值实际上是nil而我的循环正在添加到数组中的这些nil值。 所以,这次我在循环之外启动数组时没有设置大小。

我的解决方案(模仿Cary Swoveland所做的):

 #allocates new empty array of participants
participants = Array.new() 

#loops 4 times to populate embedded hash with values 
num_participants.times do
#creates the empty hash of bakesale participants
  participant = Hash.new
  puts "\n\e[36mParticipant name:\e[0m"
  name = gets.chomp
  participant["participant_name"] = name
  ["cookies", "cakes"].each do |item|
    sleep 0.25
    puts "\e[36mHow many #{item} did \e[0m #{name.capitalize} \e[36m sell (enter number)?\e[0m"  
    participant["#{item}_sold"] = gets.to_i
    sleep 0.25
    puts "\e[36mHow many does \e[0m #{name.capitalize} \e[36m have left (enter number)?\e[0m"  
    participant["#{item}_left"] = gets.to_i
  end
#loops through 4 times to add each participant to participants array
    participants << participant
  sleep 0.5
end