Never been to DZone Snippets before?

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

Convert Array to Hash (See related posts)

Probably nothing new in this but here is one way of converting an Array to a Hash:

require "enumerator"

class Array
  def to_h
    Hash[*enum_with_index.to_a.flatten]
  end
end

%w{a b c d}.to_h  # =>  {"a"=>0, "b"=>1, "c"=>2, "d"=>3}



You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts