Created
January 31, 2012 21:08
-
-
Save sdeming/1712924 to your computer and use it in GitHub Desktop.
AutoVivifying OpenStruct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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