Pete Yandell
2005-Oct-11 07:32 UTC
require_dependency and ''already initialized constant'' warning
In a module that I''m including with require_dependency, I define a couple of constants thusly: module Example EXAMPLE_CONST = 42 end (Names have been changed to protect the guilty.) On every request, Webrick spits out: ./script/../config/..//lib/example.rb:19: warning: already initialized constant EXAMPLE_CONST What''s the best way to avoid this problem, but keep the constants encapsulated within the module? Cheers, Pete Yandell
Stefan Kaes
2005-Oct-11 08:02 UTC
Re: require_dependency and ''already initialized constant'' warning
Pete Yandell wrote:> In a module that I''m including with require_dependency, I define a > couple of constants thusly: > > module Example > EXAMPLE_CONST = 42 > end > > (Names have been changed to protect the guilty.) > > On every request, Webrick spits out: > > ./script/../config/..//lib/example.rb:19: warning: already > initialized constant EXAMPLE_CONST > > What''s the best way to avoid this problem, but keep the constants > encapsulated within the module?I would use something like this module Example remove_const :EXAMPLE_CONST if Example.const_defined?(:EXAMPLE_CONST) EXAMPLE_CONST = 42 end This enables editing the relevant source file and get the new value for EXAMPLE_CONST if that changes, but maybe there is a better way to code that. You can simplify it to module Example EXAMPLE_CONST = 42 unless const_defined?(:EXAMPLE_CONST) end if you resort to restarting the server when the consts change (which won''t happen very often, probably) HTH, -- stefan kaes
Trevor Squires
2005-Oct-11 08:11 UTC
Re: require_dependency and ''already initialized constant'' warning
Something like this perhaps? module Example unless const_defined? :EXAMPLE_CONST EXAMPLE_CONST = 42 LUCKY_CONST = 7 end end HTH, Trevor On 11-Oct-05, at 12:32 AM, Pete Yandell wrote:> In a module that I''m including with require_dependency, I define a > couple of constants thusly: > > module Example > EXAMPLE_CONST = 42 > end > > (Names have been changed to protect the guilty.) > > On every request, Webrick spits out: > > ./script/../config/..//lib/example.rb:19: warning: already initialized > constant EXAMPLE_CONST > > What''s the best way to avoid this problem, but keep the constants > encapsulated within the module? > > Cheers, > > Pete Yandell > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails