solutions to the annoying “warning: already initialized constant” message

前端 未结 3 1814
抹茶落季
抹茶落季 2021-01-30 08:23

Today I\'ve stumbled upon a tricky issue with Ruby constants. In our team someone created a module, which is included into multiple models. In our (spec) test output this result

3条回答
  •  一个人的身影
    2021-01-30 08:40

    I encountered this same problem today and found a simple solution.

    Since the warning is from trying to reassign a constant with its same value, I just changed

    module LifeCycle
      RESET = 'reset'
    end
    

    to

    module LifeCycle
      RESET ||= 'reset'
    end
    

    This took care of the warning and is a lot simpler than checking if each constant is defined. Let me know if you find a better solution.

提交回复
热议问题