lib/dentaku/calculator.rb in dentaku-1.2.1 vs lib/dentaku/calculator.rb in dentaku-1.2.2

- old
+ new

@@ -37,38 +37,42 @@ @result = @evaluator.evaluate(expr.tokens) end end def solve!(expression_hash) + expressions = Hash[expression_hash.map { |k,v| [k.to_s, v] }] + # expression_hash: { variable_name: "string expression" } # TSort thru the expressions' dependencies, then evaluate all - expression_dependencies = Hash[expression_hash.map do |var, expr| + expression_dependencies = Hash[expressions.map do |var, expr| [var, dependencies(expr)] end] + variables_in_resolve_order = DependencyResolver::find_resolve_order( expression_dependencies) - results = {} - variables_in_resolve_order.each do |var_name| - results[var_name] = evaluate!(expression_hash[var_name], results) + results = variables_in_resolve_order.each_with_object({}) do |var_name, r| + r[var_name] = evaluate!(expressions[var_name], r) end - results + expression_hash.each_with_object({}) do |(k, _), r| + r[k] = results[k.to_s] + end end def dependencies(expression) Expression.new(expression, @memory).identifiers end def store(key_or_hash, value=nil) restore = @memory.dup - if !value.nil? - @memory[key_or_hash.to_sym] = value - else - key_or_hash.each do |key, value| - @memory[key.to_sym] = value + if value.nil? + key_or_hash.each do |key, val| + @memory[key.downcase.to_s] = val end + else + @memory[key_or_hash.to_s] = value end if block_given? result = yield @memory = restore