How do I turn an Array into a Hash with values of 0
without an each
loop.
For example, given this array:
[1, 2, 3, 4]
I want to get this hash:
{"1"=>0, "2"=>0, "3"=>0, "4"=>0}
How do I turn an Array into a Hash with values of 0
without an each
loop.
For example, given this array:
[1, 2, 3, 4]
I want to get this hash:
{"1"=>0, "2"=>0, "3"=>0, "4"=>0}
The standard approach is Hash[...]:
Or
Enumerable#mash
if you happen to use Facets. I cannot think of something more concise and declarative:I'm a fan of simple, and I can never remember exactly how crazy things
#inject
or Hash constructor arguments work.or in a more clean way (thanks to tokland, see the discussion in the comments)
Okay, in reality, I'd use
each_with_object
, but posting this since it's more fun.