Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created November 20, 2014 04:57
Show Gist options
  • Save KamilLelonek/f117d6d24a7f3fd870f2 to your computer and use it in GitHub Desktop.
Save KamilLelonek/f117d6d24a7f3fd870f2 to your computer and use it in GitHub Desktop.
Ruby hash quirks - part 3
[1] (pry) main: 0> my_hash = Hash.new { |hash, key| hash[key] = [] }
=> {}
[2] (pry) main: 0> my_hash[:hobbies] << 'swimming';
[3] (pry) main: 0> my_hash[:skills] << 'java';
[4] (pry) main: 0> my_hash[:hobbies]
=> ["swimming"]
[5] (pry) main: 0> my_hash[:hobbies].object_id
=> 70144893987080
[6] (pry) main: 0> my_hash[:skills]
=> ["java"]
[7] (pry) main: 0> my_hash[:skills].object_id
=> 70144900036380
[8] (pry) main: 0> my_hash[:some_random_key]
=> []
[9] (pry) main: 0> my_hash[:some_random_key].object_id
=> 70144899955360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment