From: Louis-Philippe Date: 2009-06-05T02:14:17+09:00 Subject: Re: block as string --0016363b85b04a39a2046b88e62e Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Thanks guys, while I think RubyMacro is a great idea (!!!), I will wait until it get a bit more maturity and speed before using it. I'll probably stick one of your trick onto a block Brian, something like: ----------------------------- $var = "global var" def parsed_execution(list) cmd_array = list.split("\n").map { |i| i.chomp.lstrip.rstrip } # process cmd_array here cmd_array.each_with_index do |o,i| puts "cmd #{i}:" $var = "Finished!!" if i == cmd_array.size-1 eval o end end parsed_execution <<-BLOCK puts "Line 1" puts $var puts "replacing global var..." $var = "new global var" # will never get printed!!! puts $var BLOCK --------------------------------- I choosed the <<-EOF string because it is less likely to introduce conflict with the inner syntax... though %Q with a rightly choosed symbol might also be interesting. what would be even neater would be to alias the <<- or %Q as something smaller or more intuitive for my DSL, but I believe this is impossible... Thanks! L-P 2009/6/3 Caleb Clausen > On 6/3/09, Louis-Philippe wrote: > > what I need is some sort of macro system (the LISP macro, not the C one), > > to parse the block before calling it. I was thinking to make it a > string, > > then parsing with regex, would have done it... but seems not. I'll work > out > > an other logic around the problem then ;). > > RubyMacros is an implementation of lisp-like macros for ruby.... > however, unlike ParseTree, it doesn't allow you to get at the contents > of a block. It does however, provide lisp-like forms as well. Maybe > you can put whatever code you need into a form? Take a look here: > > http://github.com/coatl/rubymacros > > --0016363b85b04a39a2046b88e62e--