Skip to content

Commit

Permalink
Do not need to store class on Dentaku::AST
Browse files Browse the repository at this point in the history
Repeated addition of many functions will gradually increase the number
of consts attached to Dentaku::AST.  This works just as well if these
remain anonymous classes.

Closes #109
  • Loading branch information
project-eutopia authored and rubysolo committed Feb 25, 2017
1 parent dd756aa commit d1beb5f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
4 changes: 0 additions & 4 deletions lib/dentaku/ast/function_registry.rb
Expand Up @@ -36,10 +36,6 @@ def type
end
end

function_class = name.to_s.capitalize.gsub('!', '_')
Dentaku::AST.send(:remove_const, function_class) if Dentaku::AST.const_defined?(function_class, false)
Dentaku::AST.const_set(function_class, function)

function.implementation = implementation
function.type = type

Expand Down
3 changes: 2 additions & 1 deletion spec/external_function_spec.rb
Expand Up @@ -62,10 +62,11 @@
it 'does not store functions across all calculators' do
calculator1 = Dentaku::Calculator.new
calculator1.add_function(:my_function, :numeric, ->(x) { 2*x + 1 })
expect(calculator1.evaluate("1 + my_function(2)")). to eq (1 + 2*2 + 1)

calculator2 = Dentaku::Calculator.new
calculator2.add_function(:my_function, :numeric, ->(x) { 4*x + 3 })

expect(calculator1.evaluate("1 + my_function(2)")). to eq (1 + 2*2 + 1)
expect(calculator2.evaluate("1 + my_function(2)")). to eq (1 + 4*2 + 3)

expect{Dentaku::Calculator.new.evaluate("1 + my_function(2)")}.to raise_error(Dentaku::ParseError)
Expand Down

0 comments on commit d1beb5f

Please sign in to comment.