ruby - Merge hash with array values to one array -


how merge hash array values 1 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 same keys, of course. reason need flatten here because values arrays, h.values alone return [["one1", "one2"], ["two1", "two2"]].

also, fyi, merge means different (and pretty useful) in ruby.

if want make sure flattens 1 level (per @tokland's comment), can provide optional argument flatten such flatten(1).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -