I have a little application helper class called TimeSpan, for converting a
number of seconds (i.e. a time difference) into a string of the form: "5h
3m
42s". My class includes constants for seconds/minute, minutes/hour,
hours/day, etc.:
module ApplicationHelper
class TimeSpan < Time
Minute = 60
Hour = Minute * 60
Day = Hour * 24
Week = Day * 7
...
end
...
end
However, running under FastCGI, every time I load a page I get warnings in my
HTTP log about constants already being initialized:
FastCGI: server "public/dispatch.fcgi" stderr:
app/helpers/application_helper.rb:9: warning: already initialized constant Week
I assume this its running in the FastCGI loop that''s causing it to try
and
reinitialize the constants every time. Is there a different (more
appropriate?) way of defining constants to avoid this?
Thanks ...
-- Steve
> However, running under FastCGI, every time I load a page I get > warnings in my > HTTP log about constants already being initialized: > > FastCGI: server "public/dispatch.fcgi" stderr: > app/helpers/application_helper.rb:9: warning: already initialized > constant Week > > I assume this its running in the FastCGI loop that''s causing it > to try and > reinitialize the constants every time. Is there a different (more > appropriate?) way of defining constants to avoid this?I get the same warning''s using Webrick for my constants. Steve
Untested and unsubstantiated, but do the warnings go away when you use ||= instead of =? Brian On 4/14/05, Steve V <ruby-ChEX1j9zMF7JbC0vcoRRxNBPR1lH4CV8@public.gmane.org> wrote:> > However, running under FastCGI, every time I load a page I get > > warnings in my > > HTTP log about constants already being initialized: > > > > FastCGI: server "public/dispatch.fcgi" stderr: > > app/helpers/application_helper.rb:9: warning: already initialized > > constant Week > > > > I assume this its running in the FastCGI loop that''s causing it > > to try and > > reinitialize the constants every time. Is there a different (more > > appropriate?) way of defining constants to avoid this? > > I get the same warning''s using Webrick for my constants. > > Steve > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
> Untested and unsubstantiated, but do the warnings go away when you use > ||= instead of =?Doing ||= nets a NameError "uninitialized constant <CONST_NAME>" message. Try to assign to the constant using =? nets a SyntaxError. Steve
I haven''t seen this done in a super pretty way, but you could add this
to your environment.rb:
class Module
alias :old_const_set :const_set
def const_set(symbol, obj)
if const_defined?(symbol)
remove_const(symbol)
end
old_const_set(symbol, obj)
end
end
Then just make sure you assign your constants using const_set:
module ApplicationHelper
class TimeSpan < Time
const_set(:Minute, 60)
const_set(:Hour, Minute * 60)
const_set(:Day, Hour * 24)
const_set(:Week, Day * 7)
...
end
...
end
Anything more elegant would be greatly appreciated.
Cheers,
Ben
On 4/15/05, Steve V
<ruby-ChEX1j9zMF7JbC0vcoRRxNBPR1lH4CV8@public.gmane.org>
wrote:>
> > Untested and unsubstantiated, but do the warnings go away when you use
> > ||= instead of =?
>
> Doing ||= nets a NameError "uninitialized constant
<CONST_NAME>" message.
> Try to assign to the constant using =? nets a SyntaxError.
>
> Steve
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>