Search code examples
rubyrubygemsconstantsgemfile

Gemfile constants redefinition, "warning: already initialized constant"


In my Gemfile I have two gems that both define the same set of constants. The two gems are sentiment_lib and engtagger.

This means whenever I run a task I get more than 10 warnings like this:

...engtagger/porter.rb:6: warning: already initialized constant Stemmable::STEP_2_LIST
...stemmer/porter.rb:10: warning: previous definition of STEP_2_LIST was here

Since the same file is just used in both it's not been an issue - but the warnings are starting to get annoying.

What can I do to fix this?


Solution

  • I would start by forking engtagger Then remove the file with the constant that's already been initiated through the other gem, commit your changes to the newly created fork.

    Add it to your Gemfile

    gem engtagger, github: 'your_github_handle/engtagger'
    

    Then run bundle update engtagger

    Now you should have engtagger in your bundle without that module.

    If we weren't dealing with constants, I would recommend a module eval, but it most likely won't work, since constants once set, can't be change, hence the warning message.