Ruby/Reflection/eval

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Bindings

def binding_elsewhere
  x = 20
  return binding
end
remote_binding = binding_elsewhere
x = 10
eval("puts x")
eval("puts x", remote_binding)
eval("x = 10")
eval("x = 50", remote_binding)
eval("puts x")
eval("puts x", remote_binding)



Dynamic Code Execution

puts eval("2 + 2")



uses strings and interpolation with Dynamic Code Execution

my_number = 15
my_code = %Q{#{my_number} * 2}
puts eval(my_code)