Ruby 从数组填充哈希
我有这个阵列:Ruby 从数组填充哈希,ruby,arrays,ruby-on-rails-3,ruby-on-rails-4,hash,Ruby,Arrays,Ruby On Rails 3,Ruby On Rails 4,Hash,我有这个阵列: params[:types] = [type1, type2, type3...] 我希望使用上述数组以以下方式填充哈希: params[:hash] = { "type1" => { something: something }, "type2" => { something: something
params[:types] = [type1, type2, type3...]
我希望使用上述数组以以下方式填充哈希:
params[:hash] = {
"type1" => {
something: something
},
"type2" => {
something: something
},
}
对于i…params[:types]中的索引使用类似于
的循环的,
只需使用数组中的最后一个值填充哈希。您可以使用each_with_object
方法执行此操作:
params = {}
params[:types] = ["type1", "type2", "type3"]
params[:types].each_with_object({}) { |k, h| h[k] = { "something" => "something" } }
最后一行将返回:
=> {"type1"=>{"something"=>"something"}, "type2"=>{"something"=>"something"}, "type3"=>{"something"=>"something"}}
您可以使用each_with_object
方法执行此操作:
params = {}
params[:types] = ["type1", "type2", "type3"]
params[:types].each_with_object({}) { |k, h| h[k] = { "something" => "something" } }
最后一行将返回:
=> {"type1"=>{"something"=>"something"}, "type2"=>{"something"=>"something"}, "type3"=>{"something"=>"something"}}
下面是一个代码片段示例,可以满足您的需要
hash = {}
array.each do |a|
hash[a.to_s] = { "something" => "something" }
end
输出:
hash
=> {
"type1" => {
"something" => "something"
},
"type2" => {
"something" => "something"
},
"type3" => {
"something" => "something"
}
}
下面是一个代码片段示例,可以满足您的需要
hash = {}
array.each do |a|
hash[a.to_s] = { "something" => "something" }
end
输出:
hash
=> {
"type1" => {
"something" => "something"
},
"type2" => {
"something" => "something"
},
"type3" => {
"something" => "something"
}
}
您可以这样做:
params = { types: ["type1", "type2", "type3"] }
Hash[params[:types].product([{"something" => "something"}])]
#=> {"type1"=>{"something"=>"something"},
# "type2"=>{"something"=>"something"},
# "type3"=>{"something"=>"something"}}
或者使用Ruby 2.1
params[:types].product([{"something" => "something"}]).to_h
如果希望为参数[:type]
的每个元素使用不同的哈希:
hashes = [{ "something1"=>"something1" }, { "something2"=>"something2" },
{ "something3"=>"something3" }]
然后
您可以这样做:
params = { types: ["type1", "type2", "type3"] }
Hash[params[:types].product([{"something" => "something"}])]
#=> {"type1"=>{"something"=>"something"},
# "type2"=>{"something"=>"something"},
# "type3"=>{"something"=>"something"}}
或者使用Ruby 2.1
params[:types].product([{"something" => "something"}]).to_h
如果希望为参数[:type]
的每个元素使用不同的哈希:
hashes = [{ "something1"=>"something1" }, { "something2"=>"something2" },
{ "something3"=>"something3" }]
然后
您没有尝试过任何东西吗?您没有尝试过任何东西吗?请使用每个带有\u对象的\u,而不是注入
。它会产生更干净的代码:params[:types]。每个带有{u object({})的{u对象({k,h{h[k]={“something”=>“something”}}
,因为不需要在块的末尾返回哈希值<代码>每个带有对象的对象都知道重用该值。啊,酷!谢谢你的提示,刚刚学到了一些东西!我将更新我的答案。无需添加“编辑:…”注释。在我对您答案的评论和我们查看您答案更改的编辑历史记录的能力之间,我们可以找出所说的内容。(关于堆栈溢出的所有问题和答案都有一个修订历史记录,高于某个声誉的用户可以看到。它有时非常方便。)再次编辑,在这种情况下:)使用而不是inject
,使用每个带有对象的\u
。它会产生更干净的代码:params[:types]。每个带有{u object({})的{u对象({k,h{h[k]={“something”=>“something”}}
,因为不需要在块的末尾返回哈希值<代码>每个带有对象的对象都知道重用该值。啊,酷!谢谢你的提示,刚刚学到了一些东西!我将更新我的答案。无需添加“编辑:…”注释。在我对您答案的评论和我们查看您答案更改的编辑历史记录的能力之间,我们可以找出所说的内容。(所有关于堆栈溢出的问题和答案都有一个修订历史记录,高于某个声誉的用户可以看到。它有时非常方便。)再次编辑,在这种情况下:)假设a.to_s
意味着将变量type1
的值转换为字符串,那么就没有理由断定这是“type1”
。我认为,最好是假设OP意味着参数[:types]=[“type1”、“type2”、“type3”…]
并将放到
。假设a.to\u
意味着将变量type1
的值转换为字符串,那么就没有理由断定这是“type1”
。我认为,最好是假设OP的意思是参数[:types]=[“type1”、“type2”、“type3”…]
并将拖放到
。