[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

DSL question

Joe Van Dyk

6/24/2005 10:57:00 PM

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?

Thanks,
Joe


2 Answers

Joel VanderWerf

6/25/2005 8:10:00 PM

0

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:

class Application
def title(*args)
case args.size
when 1
@title = args[0]
when 0
@title
end
end

def initialize(&block)
instance_eval(&block)
end
end

a = Application.new do
title "Foo"
end

puts a.title # ==> Foo



Joe Van Dyk

6/25/2005 9:23:00 PM

0

On 6/25/05, Joel VanderWerf <vjoel@path.berkeley.edu> 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