Skip to content

Instantly share code, notes, and snippets.

@sdeming
Created January 31, 2012 21:08
Show Gist options
  • Save sdeming/1712924 to your computer and use it in GitHub Desktop.
Save sdeming/1712924 to your computer and use it in GitHub Desktop.
AutoVivifying OpenStruct
module AutoVivifying
def method_missing name, *args
if name.to_s !~ /\=$/
self.send(:"#{name}=", self.class.new)
else
super
end
end
end
require 'ostruct'
class AVOpenStruct < OpenStruct
include AutoVivifying
end
sweedish = AVOpenStruct.new
sweedish.chef.bork.bork.bork = 'bork!'
puts sweedish.chef.bork.bork.bork
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment