Class: DynamicLocals::EvalTranslator
- Inherits:
-
BaseTranslator
- Object
- BaseTranslator
- DynamicLocals::EvalTranslator
- Defined in:
- lib/dynamic_locals/eval_translator.rb
Overview
This is (probably) the simplest possible implementation of the behaviour we want. It builds a new string of Ruby code with assignments to the locals each time it is called and evals that string.
This is not intended to be used in production code, but for debugging and as a reference implementation.
Instance Attribute Summary
Attributes inherited from BaseTranslator
#known_locals, #locals_hash, #lookup_strategy, #original_src
Instance Method Summary collapse
Methods inherited from BaseTranslator
#initialize, #parameters, #to_s
Constructor Details
This class inherits a constructor from DynamicLocals::BaseTranslator
Instance Method Details
#translate ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/dynamic_locals/eval_translator.rb', line 11 def translate # `eval` compiles its argument as a standalone program, which -- unlike a # method body -- has no implicit `begin`. Wrap the source in begin/end so a # method-body-style implicit rescue parses. body = "begin\n#{original_src}\nend" # The file/line arguments make errors report the line the source would # occupy had it been spliced in directly: the `begin` line lands one # line above the eval call, so source line N reports as call line + N. %{eval(#{locals_hash}.keys.map { |k| "\#{k} = #{locals_hash}[\#{k.inspect}];" }.join + #{body.inspect}, binding, __FILE__, __LINE__ - 1)} end |