# Using application\_helper.rb

**URL:** <https://www.ruby-forum.com/t/using-application-helper-rb/64477>\
**Category:** Rails\
**Created:** [June 26, 2006, 7:55am UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477 "2006-06-26T07:55:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 26, 2006, 7:55am UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/1 "2006-06-26T07:55:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 26, 2006, 9:00am UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/2 "2006-06-26T09:00:50Z")

</div>

On Jun 26, 2006, at 12:51 AM, Craig W. wrote:

> needed.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

YES\_NO = [  
[“Yes”, true],  
[“No”, false]  
].freeze unless defined? YES\_NO

-Ezra

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 26, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/3 "2006-06-26T16:37:05Z")

</div>

On 6/26/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 26, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/4 "2006-06-26T16:49:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 26, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/5 "2006-06-26T17:16:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 27, 2006, 5:31am UTC](https://www.ruby-forum.com/t/using-application-helper-rb/64477/6 "2006-06-27T05:31:11Z")

</div>

On Mon, Jun 26, 2006 at 09:35:02AM -0500, Rob S. wrote:

> On 6/26/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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ñremoved\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid
