Module: TraceEval

Defined in:
lib/trace_eval.rb,
lib/trace_eval/version.rb

Overview

Use trace point to print lines of code during eval. A randomized sigil value is used to only print lines from this invocation of eval, but nonsense could still ensure if this eval also evals.

Constant Summary collapse

VERSION =
'0.1.5'

Instance Method Summary collapse

Instance Method Details

#trace_eval(code) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/trace_eval.rb', line 10

def trace_eval(code)
  __wzpxmnrjafemvlrwflblbbyenywk__ = rand
  lines = [
    "__wzpxmnrjafemvlrwflblbbyenywk__ = #{__wzpxmnrjafemvlrwflblbbyenywk__}\n"
  ] + code.lines

  tp = TracePoint.new(:line) do |tp|
    next unless tp.path == '(eval)'
    next unless tp.binding.local_variable_get(
      :__wzpxmnrjafemvlrwflblbbyenywk__
    )==__wzpxmnrjafemvlrwflblbbyenywk__
    $stderr.print lines[tp.lineno]
  rescue NameError
  end

  begin
    tp.enable
    eval code
  ensure
    tp.disable
  end
end