Skip to content

Instantly share code, notes, and snippets.

@trans
Created June 24, 2009 09:22
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 trans/135115 to your computer and use it in GitHub Desktop.
Save trans/135115 to your computer and use it in GitHub Desktop.
YAMLStruct
# Turn a YAML document into an object via method_missing delegation.
# This is a pretty useless idea since you can just use OpenStruct.
#
# OpenStruct.new(YAML.load(yaml))
#
class YAMLStruct < BasicObject
def initialize( stream )
@yaml = YAML::load( stream )
end
def method_missing( sym, *args, &blk )
sym = sym.to_s
if sym.slice(-1,1) == '='
@yaml[ sym ] = args[0]
elsif @yaml.key?( sym )
@yaml[ sym ]
else
super
end
end
def to_s
@yaml.to_yaml
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment