Skip to content

Instantly share code, notes, and snippets.

@dekz
Created November 10, 2012 00:37
Show Gist options
  • Select an option

  • Save dekz/4049240 to your computer and use it in GitHub Desktop.

Select an option

Save dekz/4049240 to your computer and use it in GitHub Desktop.
autovivify
z = {}
z[:b][:c][:d] = 'e'
# NoMethodError: undefined method `[]' for nil:NilClass
hash_new = proc { Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) } }
a = hash_new.call
a[:b][:c][:d] = 'e'
a
# => {:b=>{:c=>{:d=>"e"}}}
@525c1e21-bd67-4735-ac99-b4b0e5262290

Copy link
Copy Markdown

Hell yeah. I've been using this too http://livescript.net/#property-access

@525c1e21-bd67-4735-ac99-b4b0e5262290

Copy link
Copy Markdown

I thought ostruct could do this anyway but it can't. At least in whatever backs http://repl.it

What ruby are you targeting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment