开发者

How to simplify this in Ruby?

开发者 https://www.devze.com 2023-02-15 09:08 出处:网络
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|

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消