开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜