用户在Ruby中生成嵌套字典(哈希)

时间:2012-01-29 13:54:07

标签: ruby hash dictionary

请原谅我的无知,但我将如何开始创建以下类型的字典:

"please give your new library a name"
French

我想要的是然后创建一个新的哈希值作为键法语{ "French" => "{}" }

的值

然后在关键法语的值中,它可以更新为{ {"hello" => "bonjour", "bonjour" => "hello"}, {"good" => "bon", "bon" => "good}, ... }

我希望你能看到我想要做的事情。也许我会以完全错误的方式解决这个问题?

编辑......也许是这些方面的东西?

puts "please give your new library a name"
user_libs = Hash.new
sub = gets.chomp
user_libs[sub] = {}
puts user_libs

1 个答案:

答案 0 :(得分:1)

print "please give your new library a name: "
name = gets.chomp
h = {name => {}}
print "enter word pairs: " #hello bonjour
while (input = gets.chomp) != ""
  k,v = input.split(" ")
  h[name][k] = v
end
p h