# Creating a DSL: adding random methods

**URL:** <https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081>\
**Category:** Ruby\
**Created:** [December 7, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081 "2008-12-07T16:05:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081/1 "2008-12-07T16:05:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081/2 "2008-12-07T17:15:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081/3 "2008-12-07T18:33:43Z")

</div>

Based on what I’m seeing here, it looks as though you may want something  
like the publisher library.

[http://atomicobjectrb.rubyforge.org/publisher/](http://atomicobjectrb.rubyforge.org/publisher/)

Not quite sure how to answer your question though. HTH  
/Shawn

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 7, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/creating-a-dsl-adding-random-methods/155081/4 "2008-12-07T18:57:11Z")

</div>

Hi –

On Sun, 7 Dec 2008, dc wrote:

> “hi this is fred!”  
> def event evt, &block  
> puts “event: #{evt} , #{event}”

Right there is where you seem to be calling event without an argument  
(inside the string interpolation). I suspect that’s where you’re  
getting the “Wrong number of arguments” error.

> after loading the individual fred.rb file in
> 
> to be the code block after do.  
> perhaps i need to yield something back to the caller?
> 
> appreciate any tips on this!

You can’t use a string representation of a block as code. Instead,  
you’d want to do something like this:

def event(evt, &block)  
(class \<\< self; self; end).class\_eval do  
define\_method(evt, &block)  
end  
end

so that you can get the local variable evt and the block into the  
method definition. (This isn’t a complete example but it might point  
you in a useful direction.)

David
