Skip to content

Instantly share code, notes, and snippets.

@hyfather
Created October 24, 2011 12:41
Show Gist options
  • Save hyfather/1308928 to your computer and use it in GitHub Desktop.
Save hyfather/1308928 to your computer and use it in GitHub Desktop.
Demonstrates how to form Hashes from Arrays in Ruby.
{:a => 'alpha'}.to_a
#=> [[:a, "alpha"]] # Notice the nested array
Hash[:a => 'alpha']
#=> {:a => 'alpha'}
Hash[[:a => 'alpha']]
#=> {} # Doesn't work with a nested array.
Hash[[[:a => 'alpha']]]
#=> {:a => 'alpha'} # Works with a doubly nested array.
# Notice that this is NOT a treble nested array.
# The outermost [] is the method notation that class:Hash accepts.
# So, it is a doubly nested array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment