Created
October 24, 2011 12:41
-
-
Save hyfather/1308928 to your computer and use it in GitHub Desktop.
Demonstrates how to form Hashes from Arrays in Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{: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