Created
February 7, 2012 19:31
-
-
Save rondale-sc/1761399 to your computer and use it in GitHub Desktop.
Tip for @peterc's ruby trickshot
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
# Two tips: | |
# Initialize a Hash with a default value of Array. | |
hash = Hash.new {|hash,key| hash[key] = []} | |
hash["some-key"] << "some-string value" | |
pp hash | |
# => {"some-key"=>["some-string value"]} | |
---------------------------------------------------------- | |
# Sort a hash by key. | |
hash = {"a" => "a-value", "c" => "c-value", "b" => "b-value"} | |
Hash[hash.sort] | |
# => {"a"=>"a-value", "b"=>"b-value", "c"=>"c-value"} | |
# They are simple but incredibly useful. |
Awesome. Glad it was helpful. As an aside I really liked the screencast you did for ruby trick shots (especially Array#zip). Keep it up.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I already had the first but am loving the second! I shared it on the book's Twitter account already but will be sure to include and credit you.