Skip to content

Calyx cannot handle string methods that accept parameters #20

@tra38

Description

@tra38

The good news is that I am right now using Calyx in a real-world project. The bad news is that doing so means you quickly encounter edge cases...


Sometimes, your program has to generate sentences without knowing in advance if they'd be plural or not.
class CatWatch < Calyx::Grammar
   start "Today, I saw {number} cats on the way to work."
end

reporter = CatWatch.new
reporter.generate(number: 1)
#=>"Today, I saw 1 cats on my way to work."

I thought about doing some monkey-patching to handle this sort of pluralization, with plans to eventually refactor to a "best practice" solution using modifiers...

class String
   def pluralize(possible_word)
      if self.to_i == 1
         "#{self} #{possible_word}"
      else
         "#{self} #{possible_word}s"
      end
   end
end

"1".pluralize("cat")
#=>"1 cat"

"2".pluralize("cat")
#=>"2 cats"

However, this monkey-patched solution doesn't work in Calyx.

class CatWatch < Calyx::Grammar
   start "Today, I saw {number.pluralize('cat')} on the way to work."
end

reporter = CatWatch.new
reporter.generate(number: 1)
=>"Today, I saw {number.pluralize('cat')} on the way to work."

Refactoring to "best practice" doesn't work either.

module Pluralize
   def pluralize(possible_word)
      if self.to_i == 1
         "#{self} #{possible_word}"
      else
         "#{self} #{possible_word}s"
      end
   end
end

class CatWatch < Calyx::Grammar
   modifier Pluralize
   start "Today, I saw {number.pluralize('cat')} on the way to work."
end

reporter = CatWatch.new
reporter.generate(number: 1)
#=>Today, I saw {number.pluralize('cat')} on the way to work.

Even more oddly, if I do a slight change to the start method...it is no longer gets interpreted as part of the string...even though I still get incorrect output.

class CatWatch < Calyx::Grammar
   modifier Pluralize
   start "{number.pluralize('cat')}"
end

reporter = CatWatch.new
reporter.generate(number: 1)
#=>"1"

Now, since I'm on a tough deadline, I wind up doing this solution instead to handle pluralization.

class String
   def pluralize_cats
      if self.to_i == 1
         "#{self} cat"
      else
         "#{self} cats"
      end
   end
end

class CatWatch < Calyx::Grammar
   start "Today, I saw {number.pluralize_cats} on the way to work."
end

reporter = CatWatch.new
reporter.generate(number: 1)
#=>"Today, I saw 1 cat on the way to work."

...which "works", but is pretty hacky. There has to be a better solution (one that hopefully doesn't involve using meta programming to generate a bunch of unique methods for handling pluralization). It'll do for now though.

I decided to follow best practice by moving my pluralize_cats method over to a module, but I then encountered a different issue with modifiers (which I reported in #21).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions