1. 2 years ago

    “already initialized constant” Warnings

    I hate it when a plugin or gem decides to override some constants that conflict with Rails or another plugin or gem. Even when this is done in a way that doesn’t cause any problems, you are forever saddled with warnings whenever Rails starts up. For example, here was the startup message for a project of mine:

    /vendor/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession

    /vendor/gems/tlsmail-0.0.1/lib/net/pop.rb:687: warning: already initialized constant POP

    /vendor/gems/tlsmail-0.0.1/lib/net/pop.rb:688: warning: already initialized constant POPSession

    /vendor/gems/tlsmail-0.0.1/lib/net/pop.rb:689: warning: already initialized constant POP3Session

    /vendor/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession

    This was caused by the tlsmail gem needed to use Gmail. The fix is to modify the plugin or gem to undefine the constant before it redefines it. In the case of tlsmail, change this:



    SMTPSession = SMTP



    to this:



    Net.instance_eval {remove_const :SMTPSession} if defined?(SMTPSession)

    SMTPSession = SMTP



    For each constant that gets redefined with a warning.

    Cheers!

    gem

    gmail

    Rails

    1. snootymonkey posted this