From: Joe Van Dyk Date: 2005-06-26T06:22:56+09:00 Subject: Re: DSL question On 6/25/05, Joel VanderWerf wrote: > Joe Van Dyk wrote: > > Hi, > > > > require 'traits' > > $applications = {} > > > > class Application > > has %w{ application_id title executable options} > > end > > > > def application(application_id, &block) > > a = Application.new > > ###### What goes here? ############# > > $applications[application_id] = a > > end > > > > application :joe_app do > > title "Joe's Application" > > executable "My executable" > > options "my options" > > end > > > > application :another_app do > > title "Another App" > > executable "another exec" > > options "the options" > > end > > > > > > It should be pretty apparent to what I'm trying to do. Any ideas? > > Can I restructure this better somehow? > > instance_eval can help: > Ooh, thanks. I forgot about that one. Revised code: joe@ubuntu:~ $ cat test.rb require 'traits' $applications = {} class Application has %w{ application_id title executable options } end def application(application_id, &block) a = Application.new a.instance_eval(&block) $applications[application_id] = a end application :joe_app do title "Joe's Application" executable "My executable" options "my options" end application :another_app do title "Another App" executable "another exec" options "the options" end puts $applications[:joe_app].title joe@ubuntu:~ $ ruby test.rb Joe's Application