How to simplify this in Ruby?
Is there a nicer way to do the same ?
objects = []
outer_h.values.each do |inner_h|
inner_h.val开发者_JAVA技巧ues.each do |obj|
objects << obj
end
end
I use Ruby 1.9.2.
objects = outer_h.values.map(&:values).flatten
will work:
h = {
1 => { :foo => "bar", :bar => "baz" },
2 => { :foo => "rab", :bar => "zab" },
}
h.values.map(&:values).flatten #=> ["bar", "baz", "rab", "zab"]
How about outer_h.flatten ? I think it might only work for arrays though.
精彩评论