Created
January 4, 2009 01:30
-
-
Save sstephenson/42974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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