Skip to content

Instantly share code, notes, and snippets.

@semmin
Created September 4, 2014 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semmin/df1794d95fada73b92d0 to your computer and use it in GitHub Desktop.
Save semmin/df1794d95fada73b92d0 to your computer and use it in GitHub Desktop.
Suppose I have the following Array of tuples [ ['a',1], ['b',2], ['c','3' ] How can I convert this to a Hash? { 'a' => 1, 'b' => 2, 'c' => 3}?
a = [ ['a',1], ['b',2], ['c',3] ]
h={}
a.flatten!.each_with_index {|el, i| i % 2 == 0 ? h[el]=a[i+1] : () }
h
# => {"a"=>1, "b"=>2, "c"=>3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment