eval.in

Paste #132948

Ruby — MRI 2.0.0, pasted 10 months ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
require 'ostruct'

not_in_locals = "we don't want this"
locals = {a: 1, b: 2}
class Vars < OpenStruct
  def get_binding() binding; end
end
vars = Vars.new(locals).get_binding
p vars.eval "a"             # good, we want this
p vars.eval "b"             # good, we want this
p vars.eval "not_in_locals" # bad, it shouldn't know this

Program Output

1
2
nil

OK (0.036 sec real, 0.035 sec wall, 8 MB, 74 syscalls)

Fork