Created
December 5, 2012 14:46
-
-
Save jboursiquot/4216037 to your computer and use it in GitHub Desktop.
Adding element to array that may not yet exist
This file contains 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
# Given: | |
hash = {} | |
(('a'..'z').to_a+('a'..'z').to_a).shuffle.each do |e| | |
# represent the following two lines as a one-liner | |
hash[:"#{e}"] ||= [] | |
hash[:"#{e}"] << rand(10) | |
end | |
# one-liner for the two-liner above | |
(hash[e.to_sym] ||= []) << rand(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment