Extract all keys and values from a Hash in Ruby
My first attempt was to just concatenate all the keys and values:
hash = {a: 1, b: 2}
hash.keys + hash.values
=> [:a, :b, 1, 2]
Ruby always surprises me. There is always a method that does exactly what you need.
So I thought, what about I just call flatten
, will it work?
hash.flatten
=> [:a, 1, :b, 2]
❤️❤️❤️ And it does. ❤️❤️❤️
Tweet