Tutorial by Examples

The instance_eval method is available on all objects. It evaluates code in the context of the receiver: object = Object.new object.instance_eval do @variable = :value end object.instance_variable_get :@variable # => :value instance_eval sets self to object for the duration of the c...
Any String can be evaluated at runtime. class Example def self.foo :foo end end eval "Example.foo" #=> :foo
Ruby keeps track of local variables and self variable via an object called binding. We can get binding of a scope with calling Kernel#binding and evaluate string inside a binding via Binding#eval. b = proc do local_variable = :local binding end.call b.eval "local_variable" #=&gt...
Ruby offers define_method as a private method on modules and classes for defining new instance methods. However, the 'body' of the method must be a Proc or another existing method. One way to create a method from raw string data is to use eval to create a Proc from the code: xml = <<ENDXML ...

Page 1 of 1