Convert [: one, 1 ,: two, 2] to {: one => 1 ,: two => 2} in Ruby

What is the most Ruby-like way to convert from Array like [:one, 1, :two, 2] to Hash like {:one => 1, :two => 2} ?

+6
ruby
source share
2 answers

Here is how I would do it:

 Hash[*array] 
+16
source share
 Hash[*[:one, 1, :two, 2]] #=> {:one => 1, :two => 2} 
+1
source share

All Articles