From: Drew Olson Date: 2008-02-29T08:11:15+09:00 Subject: Re: Help with the DSL (ish) config class Drew Olson wrote: > M.W. Mitchell wrote: >> Hi, >> >> I experimenting with this config class. I'm hoping to get something >> really easy to write and I'm on to something here, well at least I >> thought I was. This class allows me to build a nested Has like: > > I threw this together in a few minutes, doesn't support nested hashes > though :( I spoke too soon, it does now :) require 'yaml' class Configuration def initialize(filename=nil) @filename = filename @hash = {} end def method_missing(name,*args,&block) if block config = Configuration.new config.instance_eval(&block) @hash[name] = config.hash_value else @hash[name] = args.first end end def store File.open(@filename,"w") do |out| out << YAML::dump(@hash) end end def hash_value @hash end end def Configuration filename=nil,&block config = Configuration.new(filename) config.instance_eval(&block) end Configuration "my_app.yaml" do a 10 b 20 c 30 d do d1 10 d2 20 end store end -- Posted via http://www.ruby-forum.com/.