Macros/little languages?

Hi,

I have just recently learned Ruby and I really enjoy it. One thing I have not found that would be nice is a facility for easily developing little languages based on Ruby, sometimes referred to as a macro facility.

I say "based on Ruby" because I would like to be able to specify that a certain block is to be interpreted as standard Ruby. For example, it might be nice if I could do something like:

defworkflow MyAction
     conditions
         <std-ruby-expr>
     end
     action
         <std-ruby-expr>
     end
     cleanup
         <std-ruby-expr>
     end
end

This might be the beginning of a general workflow system. A workflow action has certain conditions that must hold before its action can be invoked; an action that then can take place; and cleanup to take care of any loose ends.

So my questions:

(1) Are there any tools out there that do something like this now?
(2) Is there any way to get Ruby to compile a block of code programmatically without executing it? I.e. compile the code and return me the bytecodes.
(3) Given a set of bytecodes, is there a programmatic way to cause Ruby to execute them?

Thanks in advance,
steve

Steven Arnold wrote:

Hi,

I have just recently learned Ruby and I really enjoy it. One thing I
have not found that would be nice is a facility for easily developing
little languages based on Ruby, sometimes referred to as a macro facility.

I say "based on Ruby" because I would like to be able to specify that a
certain block is to be interpreted as standard Ruby. For example, it
might be nice if I could do something like:

defworkflow MyAction
    conditions
        <std-ruby-expr>
    end
    action
        <std-ruby-expr>
    end
    cleanup
        <std-ruby-expr>
    end
end

You can do something like this in ruby quite easily:

  class WorkFlowSpec
    attr_reader :name

    def initialize name
      @name = name
    end

    def conditions(&block)
      block ? @conditions = block : @conditions
    end
    def action(&block)
      block ? @action = block : @action
    end
    def cleanup(&block)
      block ? @cleanup = block : @cleanup
    end
  end

  def defworkflow(name, &block)
    wf = WorkFlowSpec.new(name)
    wf.instance_eval(&block)
    wf
  end

  wf = defworkflow "MyAction" do
    conditions do
      puts "conditions"
    end
    action do
      puts "action"
    end
    cleanup do
      puts "cleanup"
    end
  end

p wf.name # ==> "MyAction"
wf.conditions.call # ==> conditions

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Hi Steven
If I understand you correctly, what you're trying to do can easily be
accomplished using Ruby's metaprogramming capabilities. I posted a
small tutorial on the subject here
http://www.theniceweb.com/JaviersBlog/2005/08/metaprogramming-or-how-you-get-to-have.html
(there'll be a follow-on soon) Hope it helps.
Best regards

      j.