Skip to content

Instantly share code, notes, and snippets.

@13k
Last active December 18, 2015 11:39
Show Gist options
  • Select an option

  • Save 13k/5776800 to your computer and use it in GitHub Desktop.

Select an option

Save 13k/5776800 to your computer and use it in GitHub Desktop.
ActiveSupport::HashWithIndifferentAccess and initialization block
h = ActiveSupport::HashWithIndifferentAccess.new {|h,k| h[k] = [] }
# => {}
h[:key1]
# => []
h[:key1] << 13
# => [13]
h
# => {"key1"=>[13]}
h[:key2] << 31
# => [31]
h
# => {"key1"=>[13], "key2"=>[]}
h2 = Hash.new {|h, k| h[k] = [] }
# => {}
h2[:key] << 13
# => [13]
h2
# => {:key=>[13]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment