creating a DSL: adding random methods

D

dc

hi -

I'm trying to create a simple DSL, using method missing.

i want to be able to add different event logic for different
instances, defined in their own files.
trivial example:

# eg in fred.rb
event :say do
"hi this is fred!"
end

# and joe.rb
event :say do
"joe doesnt say much"
end


so i have a class to implement/create the DSL methods:

def event evt, &block
puts "event: #{evt} , #{event}"
str = "
def #{evt}
#{block}
end
"
puts str
self.class.class_eval(str)
end


after loading the individual fred.rb file in
class Brain
def self.load(filename)
dsl = new
dsl.instance_eval(File.read(filename))
end

then when the event XX do is encountered, i am getting:

ArgumentError: ./parser.rb:37:in ‘event’: wrong number of arguments (0
for 1)

# this is the method call
event :say do
puts ".code here.."
end

# calling this
def event evt, &block


i was expecting "say" to be passed as the evt (method name) and block
to be the code block after do.
perhaps i need to yield something back to the caller?

appreciate any tips on this!

/dc
 
T

Tom Link

Basically, I only wanted to say that your subject line sounded very
promising.
  def event evt, &block
    puts "event: #{evt} , #{event}"

What does this #{event} refer to?

Regards,
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top