Skip to content

Instantly share code, notes, and snippets.

@aaronblohowiak
Created May 29, 2009 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronblohowiak/119894 to your computer and use it in GitHub Desktop.
Save aaronblohowiak/119894 to your computer and use it in GitHub Desktop.
require 'ostruct'
class Pearl < OpenStruct
alias_method :open_structs_method_missing, :method_missing
def method_missing(meth, *args)
if meth.to_s =~ /=$/
open_structs_method_missing(meth, *args)
else
self.new_ostruct_member(meth)
new_child = Pearl.new(args)
self.send(meth.to_s+"=", new_child)
return new_child
end
end
end
#
irb(main):017:0> a = Pearl.new
=> #<Pearl>
irb(main):018:0> a.pants.belt.buckle.burst = true
=> true
irb(main):019:0> a
=> #<Pearl pants=#<Pearl ...>>
irb(main):020:0> a.pants
=> #<Pearl belt=#<Pearl ...>>
irb(main):021:0> a.pants.chores = 4
=> 4
irb(main):022:0> a.pants
=> #<Pearl chores=4, belt=#<Pearl ...>>
irb(main):023:0> a.pants.chores
=> 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment