Ruby/Reflection/eval

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

Версия от 17:57, 13 сентября 2010; ViGOur (Обсуждение | вклад)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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)