Convert Array to Hash
1 2 require "enumerator" 3 4 class Array 5 def to_h 6 Hash[*enum_with_index.to_a.flatten] 7 end 8 end 9 10 %w{a b c d}.to_h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}
DZone Snippets > pervel > array
12738 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
1 2 require "enumerator" 3 4 class Array 5 def to_h 6 Hash[*enum_with_index.to_a.flatten] 7 end 8 end 9 10 %w{a b c d}.to_h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}