I have a simple Yes/No selection list that I am using in a number of places. Thinking that I could DRY things out a bit, I added it to application_helper.rb ... YES_NO = [ [ "Yes", true ], [ "No", false ] ].freeze So I figured I would use it there instead of in each model.rb where needed. anyway, in webrick screen, I am continually seeing this... script/../config/../app/helpers/application_helper.rb:6: warning: already initialized constant YES_NO Am I doing something wrong? Is there a better way to do this? Craig
On Jun 26, 2006, at 12:51 AM, Craig White wrote:> I have a simple Yes/No selection list that I am using in a number of > places. Thinking that I could DRY things out a bit, I added it to > application_helper.rb ... > > YES_NO = [ > [ "Yes", true ], > [ "No", false ] > ].freeze > > So I figured I would use it there instead of in each model.rb where > needed. > > anyway, in webrick screen, I am continually seeing this... > > script/../config/../app/helpers/application_helper.rb:6: warning: > already initialized constant YES_NO > > Am I doing something wrong? Is there a better way to do this? > > Craig > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsYES_NO = [ [ "Yes", true ], [ "No", false ] ].freeze unless defined? YES_NO -Ezra
On 6/26/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:> YES_NO = [ > [ "Yes", true ], > [ "No", false ] > ].freeze unless defined? YES_NO > > -EzraSo, isn''t this just saying "only freeze if the object isn''t defined? In which case, it will never be frozen...since its always going to be defined? I think the original code was meant to make a "real" constant, tho I doubt if thats really possible in ruby. - rob -- http://www.robsanheim.com
I think that the code that Ezra put forward will declare and then freeze unless defined. Both actions will occur or neither will. One thing I don''t understand about this snippet is why would YES_NO be defined? I thought that each request is a request all of it''s own. Share nothing. Doesn''t this mean that it will only be defined when the application_helper.rb is initally loaded? Since I don''t think it''s loaded more than once I don''t understand the point of the unless defined? is. On 6/27/06, Rob Sanheim <rsanheim@gmail.com> wrote:> > On 6/26/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote: > > YES_NO = [ > > [ "Yes", true ], > > [ "No", false ] > > ].freeze unless defined? YES_NO > > > > -Ezra > > > So, isn''t this just saying "only freeze if the object isn''t defined? > In which case, it will never be frozen...since its always going to be > defined? I think the original code was meant to make a "real" > constant, tho I doubt if thats really possible in ruby. > - rob > -- > http://www.robsanheim.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060626/9d7ad081/attachment.html
For one thing, the unless defined? YES_NO addition quieted the display - which was the result I was looking for. I guess that application_helper.rb is continually reloaded in development mode (which is the current environment for this section of code) but in production, I would guess that it is only loaded once and it wouldn''t have mattered. If that is the case, I probably could have just ignored the on-screen noise but I try to quiet noise where I can. Craig On Tue, 2006-06-27 at 00:49 +1000, Daniel N wrote:> I think that the code that Ezra put forward will declare and then > freeze unless defined. Both actions will occur or neither will. > > One thing I don''t understand about this snippet is why would YES_NO be > defined? > I thought that each request is a request all of it''s own. Share > nothing. Doesn''t this mean that it will only be defined when the > application_helper.rb is initally loaded? Since I don''t think it''s > loaded more than once I don''t understand the point of the unless > defined? is. > > > On 6/27/06, Rob Sanheim <rsanheim@gmail.com> wrote: > On 6/26/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote: > > YES_NO = [ > > [ "Yes", true ], > > [ "No", false ] > > ].freeze unless defined? YES_NO > > > > -Ezra > > > So, isn''t this just saying "only freeze if the object isn''t > defined? > In which case, it will never be frozen...since its always > going to be > defined? I think the original code was meant to make a > "real" > constant, tho I doubt if thats really possible in ruby. > - rob > -- > http://www.robsanheim.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Mon, Jun 26, 2006 at 09:35:02AM -0500, Rob Sanheim wrote:> On 6/26/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote: > > YES_NO = [ > > [ "Yes", true ], > > [ "No", false ] > > ].freeze unless defined? YES_NO> So, isn''t this just saying "only freeze if the object isn''t defined?No, it''s saying "define YES_NO and freeze it unless it exists" (YES_NO = [...].freeze) unless ... Saludos! HoraPe --- Horacio J. Pe?a horape@compendium.com.ar horape@uninet.edu