Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created January 4, 2009 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstephenson/42974 to your computer and use it in GitHub Desktop.
Save sstephenson/42974 to your computer and use it in GitHub Desktop.
require "tcl"
module Tcl
class ExprEvaluator
def initialize
@interp = Tcl::SafeInterp.new
commands = @interp.list_to_array(@interp._!(:info, :commands)) - %w(info set unset rename expr)
(commands + %w(rename)).each { |command| @interp._!(:rename, command, "") }
end
def evaluate(expression, variables = {})
load!(variables)
@interp._!(:expr, expression)
end
protected
def unset_all_globals!
vars = @interp.list_to_array(@interp._!(:info, :globals))
vars.each { |var| @interp._!(:unset, var) }
end
def load!(variables)
unset_all_globals!
variables.each { |var, value| @interp._!(:set, var, value) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment