Module: Tins::Interpreter
- Included in:
- Object
- Defined in:
- lib/tins/dslkit.rb
Overview
Provides dynamic code interpretation capabilities for evaluating string-based code within the context of an object instance.
This module enables the execution of Ruby code snippets as if they were blocks, maintaining access to the current binding and instance variables.
Instance Method Summary collapse
-
#interpret(source, *args) ⇒ Object
Interpret the string source as a body of a block, while passing *args into the block.
-
#interpret_with_binding(source, my_binding, *args) ⇒ Object
Interpret the string source as a body of a block, while passing *args into the block and using my_binding for evaluation.
Instance Method Details
#interpret(source, *args) ⇒ Object
Interpret the string source as a body of a block, while passing *args into the block.
This method automatically creates a binding from the current context, making all instance variables and methods available to the interpreted code.
282 283 284 |
# File 'lib/tins/dslkit.rb', line 282 def interpret(source, *args) interpret_with_binding(source, binding, *args) end |
#interpret_with_binding(source, my_binding, *args) ⇒ Object
Interpret the string source as a body of a block, while passing *args into the block and using my_binding for evaluation.
This method allows explicit control over the binding context, enabling access to specific local variables from a particular scope.
297 298 299 300 301 302 303 304 305 |
# File 'lib/tins/dslkit.rb', line 297 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 |