> Hash[:a,2,:b,4]
=> {:a=>2, :b=>4}
> Hash[:a,1]
=> {:a=>1}
> Hash[[:a,1]]
=> {}
> Hash[[[:a,1]]]
=> {:a=>1}
You can pass the key-value pairs two ways:
Hash::[]
, with the keys and values alternatingThe first form fits 1, the second form fits 1, the fourth form fits 2, but the third form doesn't fit either (it consists of a single array, but neither :a
nor 1
is a key-value pair).
The reason the second form is useful is because that's what you tend to get from Hash's Enumerable methods — an array of key-value pairs in arrays. So you can write Hash[some_hash.map {|k, v| [k, v+1]}]
and you'll end up with a Hash transformed the way you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With