Class: WebConsole::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/web_console/evaluator.rb

Overview

Simple Ruby code evaluator.

This class wraps a Binding object and evaluates code inside of it. The difference of a regular Binding eval is that Evaluator will always return a string and will format exception output.

Instance Method Summary collapse

Constructor Details

#initialize(binding = TOPLEVEL_BINDING) ⇒ Evaluator

Returns a new instance of Evaluator.



17
18
19
# File 'lib/web_console/evaluator.rb', line 17

def initialize(binding = TOPLEVEL_BINDING)
  @binding = binding
end

Instance Method Details

#eval(input) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/web_console/evaluator.rb', line 21

def eval(input)
  # Binding#source_location is available since Ruby 2.6.
  if @binding.respond_to? :source_location
    "=> #{@binding.eval(input, *@binding.source_location).inspect}\n"
  else
    "=> #{@binding.eval(input).inspect}\n"
  end
rescue Exception => exc
  format_exception(exc)
end