Class: RVM::Interpreter::ObjectContext
- Defined in:
- lib/rvm/interpreter.rb
Overview
Jumps into object context (anther way beside using send).
The special variable :self (attention, not to be mixed wiht ‘self’) is set to the object in which context we are!
This is one of the few RVM::Interpreter calsses that is likely to be used outside a compiler as it will allow to execute a set of code within the scope of a specific object:
Example
RVM::Interpreter::ObjectContext.new(my_object, my_code).execute(env)
This will execute my_code in a way pretty much equal to instance_eval in ruby would do.
Instance Attribute Summary
Attributes inherited from Element
Instance Method Summary collapse
-
#execute(env) ⇒ Object
Execute for this Interpreter element by creating a new environment , setting it’s variables and functions to those definded by the object’s
functions
andvariables
methods. -
#initialize(object, code, pos = nil) ⇒ ObjectContext
constructor
The constructor takes 3 arguments, the first beeing the object of which the scope is taken, the second is the code to be executed in the objects scope and the third is the position, to be set by the compiler.
- #optimize ⇒ Object
- #pretty_print(q) ⇒ Object
Constructor Details
#initialize(object, code, pos = nil) ⇒ ObjectContext
The constructor takes 3 arguments, the first beeing the object of which the scope is taken, the second is the code to be executed in the objects scope and the third is the position, to be set by the compiler.
The passed object is executed once when the code is ran so it can be passed something that evaluates like a variable, or constant.
107 108 109 110 111 |
# File 'lib/rvm/interpreter.rb', line 107 def initialize object, code, pos = nil super(pos) @object = object @code = code end |
Instance Method Details
#execute(env) ⇒ Object
Execute for this Interpreter element by creating a new environment , setting it’s variables and functions to those definded by the object’s functions
and variables
methods. It also sets the :self variable in the new environment to the object.
123 124 125 126 127 128 129 |
# File 'lib/rvm/interpreter.rb', line 123 def execute env #The new obj = @object.execute(env) new_env = Interpreter::Environment.new({:functions => obj.functions, :locals => obj.variables}, env) new_env.declare(:self, obj) @code.execute(new_env) end |
#optimize ⇒ Object
131 132 133 134 135 |
# File 'lib/rvm/interpreter.rb', line 131 def optimize @object = @object.optimize @code = @code.optimize super end |
#pretty_print(q) ⇒ Object
113 114 115 116 117 |
# File 'lib/rvm/interpreter.rb', line 113 def pretty_print(q) q.group 1, "#{@object}.{", "}" do q.pp @code end end |