creating a DSL: adding random methods

Discussion in 'Ruby' started by dc, Dec 7, 2008.

  1. dc

    dc Guest

    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
    dc, Dec 7, 2008
    #1
    1. Advertising

  2. dc

    Tom Link Guest

    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
    Tom Link, Dec 7, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

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

    http://atomicobjectrb.rubyforge.org/publisher/

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

    On Sun, Dec 7, 2008 at 8:08 AM, Tom Link <> wrote:

    > 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
    >
    >
    Shawn Anderson, Dec 7, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmar Baumann
    Replies:
    0
    Views:
    598
    Elmar Baumann
    Feb 2, 2004
  2. globalrev
    Replies:
    4
    Views:
    745
    Gabriel Genellina
    Apr 20, 2008
  3. snacktime

    Creating a DSL in ruby

    snacktime, Jun 29, 2007, in forum: Ruby
    Replies:
    3
    Views:
    94
    Yossef Mendelssohn
    Jul 2, 2007
  4. Daniel Waite
    Replies:
    8
    Views:
    143
    Andrew Stewart
    May 2, 2008
  5. VK
    Replies:
    15
    Views:
    1,125
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page