Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created February 7, 2012 19:31
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 rondale-sc/1761399 to your computer and use it in GitHub Desktop.
Save rondale-sc/1761399 to your computer and use it in GitHub Desktop.
Tip for @peterc's ruby trickshot
# 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.
@peterc
Copy link

peterc commented Feb 8, 2012

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.

@rondale-sc
Copy link
Author

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