How to merge hash with array values to one array:
h = {
one: ["one1", "one2"],
two: ["two1", "two2"]
}
after merge should be:
["one1","one2","two1","two2"]
h.values.flatten
# => ["one1", "one2", "two1", "two2"]
You can do the same for the keys, of course. The only reason you need flatten here is because the values are themselves arrays, so h.values alone will return [["one1", "one2"], ["two1", "two2"]].
Also, just as an FYI, merge means something different (and pretty useful) in Ruby.
If you want to make sure it flattens only one level (per @tokland's comment), you can provide an optional argument to flatten such as with flatten(1).
flatten(1) is more precise.flatten is overused. A lots of times we only need to flatten one level and still we call a recursive flat. Maybe the OP has nested arrays, but in this case he would (should've) put it in the example. Just a detail, nothing more.h.flat_map &:last
=> ["one1", "one2", "two1", "two2"]
Are you Robot :(
["two1", "two2", "one1", "two2"]acceptable?