Skip to content

Add HumanizeVisitor and Calculator#humanize#338

Open
MaximilianoGarciaRoe wants to merge 1 commit into
rubysolo:mainfrom
MaximilianoGarciaRoe:main
Open

Add HumanizeVisitor and Calculator#humanize#338
MaximilianoGarciaRoe wants to merge 1 commit into
rubysolo:mainfrom
MaximilianoGarciaRoe:main

Conversation

@MaximilianoGarciaRoe

@MaximilianoGarciaRoe MaximilianoGarciaRoe commented May 13, 2026

Copy link
Copy Markdown

Why

Apps that expose Dentaku formulas to end users (HR tools, business-rule
engines, approval workflows) have no way to explain what a formula means
in plain language. This fills that gap.

calc = Dentaku::Calculator.new

calc.humanize('IF(tenure >= 2, base * 1.1, base)')
# => "if tenure is greater than or equal to 2 then base times 1.1 else base"

calc.humanize('days >= min and days <= max', min: 5, max: 20)
# => "days is greater than or equal to 5 and days is less than or equal to 20"

What

HumanizeVisitor < PrintVisitor — overrides only the output, inherits
everything else. Falls back to PrintVisitor for any unmapped node, so
custom functions still render.

Covers all built-in constructs: arithmetic, comparison, logical and
bitwise operators, IF / SWITCH / CASE, all numeric, string, and
collection functions, and the full Ruby Math module.

Lazy-loaded — no overhead unless #humanize is called.

Notes

  • Phrase wording ("times" vs "multiplied by", etc.) is easy to adjust.
  • i18n is out of scope but the phrase table is one hash — straightforward
    to externalize later if wanted.
  • Happy to drop Calculator#humanize if you prefer the visitor standalone.

Happy to contribute and looking forward to any feedback or suggestions!

Adds a visitor that produces a natural-language English representation of
a Dentaku expression. Operators are verbalized (>= -> 'is greater than or
equal to'), and identifiers can optionally be substituted with concrete
values via a second argument to Calculator#humanize.

  calc = Dentaku::Calculator.new
  calc.humanize('days >= min and days <= max', min: 5, max: 20)
  # => 'days is greater than or equal to 5 and days is less than or equal to 20'

HumanizeVisitor subclasses PrintVisitor, so any expression PrintVisitor
handles is also handled here; only operator output is overridden.
@DirkDoes

DirkDoes commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

be carefull what features you use of Ruby, since Dentaku even supports 2.5 which does not contain all the quality of life features.
For instance you are using args[1..] which is not supported in ruby 2.5 (or specifically the 1.. endless range)
Instead you should use args[1..-1] to not make it an endless range, or just args.drop(1)

Just realized that 2.5 will not be supported later. so i think its just matter up re-syncing it with main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants