Skip to content

Instantly share code, notes, and snippets.

@mikezter
Created September 3, 2010 17:47
Show Gist options
  • Save mikezter/564248 to your computer and use it in GitHub Desktop.
Save mikezter/564248 to your computer and use it in GitHub Desktop.
Recursive OpenStruct
require 'ostruct'
class ROpenStruct < OpenStruct
def table
@table ||= {}
end
def method_missing(mid, *args)
mname = mid.id2name
len = args.length
if len == 0
table[mname.intern] = ROpenStruct.new
self.new_ostruct_member(mname)
table[mname.intern]
else
super
end
end
end
if __FILE__ == $PROGRAM_NAME
test = ROpenStruct.new
test.p = "hello"
test.t.s.p.t.z.y.x = "hi"
test.xyz
p test
p test.p
p test.t.s.p.t.z.y.x
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment