Ruby/Reflection/eval

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

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:57, 13 сентября 2010

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)