module DSLKit::Interpreter
def interpret(source, *args)
A.new.interpret('|a,b| a + b + c', 1, 2) # => 6
end
end
3
def c
include DSLKit::Interpreter
class A
the *args can be fetched:
A small example explains how the method is supposed to be used and how
*args into the block.
Interpret the string _source_ as a body of a block, while passing
def interpret(source, *args) interpret_with_binding(source, binding, *args) end
def interpret_with_binding(source, my_binding, *args)
A.new.foo # => 6
end
end
interpret_with_binding('|a| a + b + c', binding, 1) # => 6
b = 2
def foo
end
3
def c
include DSLKit::Interpreter
class A
A small example:
*args into the block and using _my_binding_ for evaluation.
Interpret the string _source_ as a body of a block, while passing
def interpret_with_binding(source, my_binding, *args) path = '(interpret)' if source.respond_to? :to_io path = source.path if source.respond_to? :path source = source.to_io.read end block = lambda { |*a| eval("lambda { #{source} }", my_binding, path).call(*a) } instance_exec(*args, &block) end