动态哈希:在一个哈希中添加更多的哈希



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"quot;电子邮件: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散列的末尾

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

你是对的。你所需要做的就是删除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 = contacts.key?(name_key) ? contacts[name_key] : {}替换new_key_and_value = {},但如果该联系人很大,您将在内存中无端复制,而且它的描述性较差。

您可以使用以下方法。

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的最后一行是可选的。如果CCD_ 5不存在,则将根据需要对其进行修改。我把它包括在内,这样人们就可以写一些类似的东西

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

而不是

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

如果需要的话。

最新更新