From: goodieboy Date: 2008-03-15T02:09:39+09:00 Subject: Re: Help with the DSL (ish) config class Cool that's exactly what I was looking for! I came up with this simple little piece: class Config instance_methods.each { |m| undef_method m unless (m =~ /^__/) or ['class', 'instance_eval'].include?(m) } def initialize(init_data=nil, &block) @d = init_data || {} instance_eval(&block) if block_given? end def to_hash; @d; end def method_missing(method, data=nil, &block) @d[method] = data return @d[method] = self.class.new(data, &block).to_hash if data.nil? @d end end c = Config.new do simple_setting 'the value' send 'Testing send' nested { new 'Testing new, make sure it gets passed to method_missing' nested_key 'nested_value' deep_nesting { deep_nested_key 'deep nested value' o 'Test' instance_variables 'test' } } end puts c.to_hash.inspect Thanks for all of your help and suggestions. I learned quite a bit from this :) Matt On Mar 2, 12:27 pm, Drew Olson wrote: > M.W. Mitchell wrote: > > Hi, > > > Thanks Drew. I had something like that working too. But the problem is > > method_missing. What if I want to create a key that happens to have > > the same name as a method that actually exists? Also, how would you > > convert that to a hash? > > > Thanks again, > > Matt > > Matt - > > That code actually produces a hash, we are just storing it in a file. If > you wanted to grab the file and use the hash, you would do something > along the lines of: > > my_hash = YAML::load(File.read("my_file.yaml")) > > As far as the method_missing issues, this is a typical example of where > using something like BlankSlate would be helpful. You can read more > about it here:http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc > > - Drew > -- > Posted viahttp://www.ruby-forum.com/.