【问题标题】:Dynamic Hashes: adding more to a hash within a hash动态散列:在散列中添加更多散列
【发布时间】:2020-09-29 05:13:57
【问题描述】:

Ruby 初学者。在散列中添加了散列,但无法将更多键和值“添加”到散列中,而无需覆盖新散列中的第一个键和值。示例代码:

contacts = {
    "Jason" => {
      "phone" => 833,
      "twitter" => "sliceyboi",
      "email" => "crystal@lake"
    },
    "Freddy" => {
      "phone" => 666,
      "tiktok" => "asdf",
      "instagram" => "def",
      "email" => "in.your@dreams"
    }
  }

while true
puts contacts

new_key_and_value = {}

puts "add name"

name_key = gets.chomp

puts "add media:"
new_key = gets.chomp
 
puts "add #{new_key} info:"
new_value = gets.chomp

new_key_and_value[new_key] = new_value

contacts[name_key] = new_key_and_value
end

我可以添加“Chucky”“电子邮件:doll@toys”。但如果我再次检查,对于 Chucky 或其中任何一个的任何名称,我希望添加更多媒体,而不是覆盖。我知道这与创建的新哈希有关。

例如它的作用:

add name

>Chucky
add media:
>email
add email info:
> doll@toys

{"Jason"=> 
     { "phone"=>833, 
     "twitter"=>"sliceyboi", 
     "email"=>"crystal@lake"},
 
"Freddy"=>
      {"phone" => 666,
      "tiktok" => "asdf",
      "instagram" => "def",
      "email" => "in.your@dreams"}, 

"Chucky"=>
     {"email"=>"doll@toys"}
    }

add name
>Chucky
add media:
>tiktok
add tiktok info:
>chplay

{"Jason"=> 
     { "phone"=>833, 
     "twitter"=>"sliceyboi", 
     "email"=>"crystal@lake"},
 
"Freddy"=>
      {"phone" => 666,
      "tiktok" => "asdf",
      "instagram" => "def",
      "email" => "in.your@dreams"}, 

"Chucky"=>
     {"tiktok"=>"chplay"}
    }

寻找:

{"Jason"=> 
     { "phone"=>833, 
     "twitter"=>"sliceyboi", 
     "email"=>"crystal@lake"},
 
"Freddy"=>
      {"phone" => 666,
      "tiktok" => "asdf",
      "instagram" => "def",
      "email" => "in.your@dreams"}, 

"Chucky"=>
     {"email"=>"doll@toys",
     "tiktok"=>"chplay",
}
    }

这样它就会添加到 Chucky 哈希的末尾

【问题讨论】:

  • 我建议每当你给出一个例子时,你都会展示预期的结果(作为一个 Ruby 对象)。这往往对读者有帮助。

标签: ruby hash


【解决方案1】:

我知道这与创建的新哈希有关。

你是对的。您需要做的就是删除 new_key_and_value = {} 而不是您写的地方

new_key_and_value[new_key] = new_value

contacts[name_key] = new_key_and_value

# if contact doesn't exist create it.
if not contacts.key?(name_key) then contacts[name_key] = {} end

contacts[name_key][new_key] = new_value

或者,您可以将 new_key_and_value = {} 替换为 new_key_and_value = contacts.key?(name_key) ? contacts[name_key] : {},但如果该联系人很大,您会无缘无故地在内存中制作副本,而且描述性较差。

【讨论】:

    【解决方案2】:

    您可以使用以下方法。

    def add_contact(contacts)
      name = seek("What is name to add?")
      h = {}
      loop do
        key, val = add_media
        break if key == ''
        h[key] = val
      end
      contacts[name] = h
      contacts 
    end
    
    def add_media
      key = seek("Add media (enter empty string when finished")
      return key if key.empty?
      [key, seek("Add #{key} information")]
    end
    
    def seek(msg)
      print "#{msg}: "
      gets.chomp
    end
    

    如果对话是:

    add_contact contacts
    What is name to add?: Chucky
    Add media (enter empty string when finished: email
    Add email information: doll@toys
    Add media (enter empty string when finished: tiktok
    Add tiktok information: chplay
    Add media (enter empty string when finished: 
    

    该方法将返回:

    #=> { "Jason"=>{
    #       "phone"=>833,
    #       "twitter"=>"sliceyboi",
    #       "email"=>"crystal@lake"
    #     },
    #     "Freddy"=>{
    #       "phone"=>666,
    #       "tiktok"=>"asdf",
    #       "instagram"=>"def",
    #       "email"=>"in.your@dreams"
    #     },
    #     "Chucky"=>{
    #       "email"=>"doll@toys",
    #       "tiktok"=>"chplay"
    #     }
    #   } 
    

    add_contact 的最后一行是可选的。 contacts 如果不存在,将根据需要进行修改。我已经将它包含在内,以便人们可以编写类似的东西

    add_contact(contacts).each { |k,v| .... }
    

    而不是

    add_contact(contacts)
    contacts.each { |k,v| .... }
    

    这应该是需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2012-02-20
      • 2012-12-27
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多