macros/little languages?

S

Steven Arnold

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
 
J

Joel VanderWerf

Steven said:
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
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top