Class: RS::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/rs/eval.rb

Overview

Runtime environment for executing user input as Ruby.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvaluator

Create a new execution environment.

You should use RS.start instead.

TODO: Generate unique bindings?


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rs/eval.rb', line 70

def initialize()
  @binding = eval "lambda { binding }.call", TOPLEVEL_BINDING

  stash = OpenStruct.new  :evaluator => self,
                          :config => OpenStruct.new

  @main = execute "self"
  @main.instance_variable_set "@rs", stash

  # Work around MRI changing description.
  def @main.inspect(); :rs_main; end

  execute "def rs(); @rs; end"
end

Instance Attribute Details

#mainObject (readonly)

Allow accessing main from the outside.


88
89
90
# File 'lib/rs/eval.rb', line 88

def main
  @main
end

Class Method Details

.inspectObject

Work around MRI changing description.


80
# File 'lib/rs/eval.rb', line 80

def @main.inspect(); :rs_main; end

Instance Method Details

#execute(expression, file = "<no file>", line = "<no line>") ⇒ Object

Execute a presumably valid String of Ruby code.

Trying to execute an incomplete Ruby expression raises IncompleteExpression.

Errors are caught etc. (except top-level next and redo LocalJumpErrors, those need to be caught outside this scope), and returned as objects.


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rs/eval.rb', line 100

def execute(expression, file = "<no file>", line = "<no line>")
  eval expression, @binding

rescue SyntaxError => e
  case e.message
  when /(parse|syntax) error.*?\$end/i, /unterminated/i  # Should catch most
    raise IncompleteExpression
  else
    e
  end
rescue SystemExit
  raise
rescue Exception => e
  e
end