Using application_helper.rb

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 W. wrote:

needed.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

YES_NO = [
[ “Yes”, true ],
[ “No”, false ]
].freeze unless defined? YES_NO

-Ezra

On 6/26/06, Ezra Z. [email protected] 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

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.

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 Mon, Jun 26, 2006 at 09:35:02AM -0500, Rob S. wrote:

On 6/26/06, Ezra Z. [email protected] 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ñ[email protected]
[email protected]