dentaku icon indicating copy to clipboard operation
dentaku copied to clipboard

Hash parameters in custom functions

Open hashwin opened this issue 5 years ago • 2 comments

Here I have a custom function "test"

c = Dentaku::Calculator.new
c.add_function(:test, :string, ->(test_hash) { test_hash.values.join })
c.evaluate("TEST(abc)", { abc: { a: '1', b: '2', c: '3' }})

I would expect the result of this to be "123", but it is returning nil. In fact, it does not even enter the custom function if there is a hash parameter.

Is there something I am missing?

hashwin avatar Feb 21 '20 04:02 hashwin

Use c.evaluate! to throw the exception out instead of being failed silently:

c.evaluate!("TEST(abc)", { abc: { a: '1', b: '2', c: '3' }})

Dentaku::UnboundVariableError: no value provided for variables: abc
from /usr/lib/ruby/gems/2.6.0/gems/dentaku-3.3.4/lib/dentaku/calculator.rb:67:in `block in evaluate!'

And this behavior is actually the feature "nested_data" in Dentaku. For your purpose you can use:

c = Dentaku::Calculator.new(nested_data_support: false)
c.add_function(:test, :string, ->(test_hash) { test_hash.values.join })
c.evaluate!("TEST(abc)", { abc: { a: '1', b: '2', c: '3' }})
#=> "123"

To acquire your desired result.

david942j avatar Feb 21 '20 14:02 david942j

This was changed recently with https://github.com/rubysolo/dentaku/pull/203 Once a new release comes out, your code will work as is.

sliiser avatar Feb 28 '20 14:02 sliiser