Alexandre Mutel wrote:
> Woop, i was to fast. In fact, i need an eval on the whole file, because
> my dsl language allow ruby code to be used (and so definition of
> methods... etc.)
Then it sounds like you just need to separate the blocks of code
appropriately. Do you want each line which begins with \w: (a labelled
line) to be treated specially? Then the rest of the code between the
labelled lines can be treated as a single string.
Proof-of-concept:
src = <<EOS
def foo
puts "XXX"
end
label1: foo # this is a test
def bar
puts "YYY"
end
label2: bar
EOS
def label(name)
puts "Executing label #{name} now..."
yield
end
b = binding
line = 1
src.split(/^(\w+:.*)\n/).each do |chunk|
if chunk =~ /(\w+)

.*)$/
eval "label(#{$1.inspect}) { #{$2}\n }", b, "DSL", line
line += 1
else
eval chunk, b, "DSL", line
line += chunk.split("\n").size
end
end
--
Posted via
http://www.ruby-forum.com/.