# Can I put all my Ruby constants in environment.rb?

**URL:** <https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321>\
**Category:** Rails\
**Created:** [July 6, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321 "2008-07-06T20:06:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 6, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/1 "2008-07-06T20:06:15Z")

</div>

Hello, I got a lot of Ruby constants spread over a couple of models.  
Is it possible to throw them all into environment.rb instead? I’d  
really enjoy having things in one place. Thanks.

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 6, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/2 "2008-07-06T20:26:44Z")

</div>

Save The Vinyls wrote:

> Hello, I got a lot of Ruby constants spread over a couple of models.  
> Is it possible to throw them all into environment.rb instead? I’d  
> really enjoy having things in one place. Thanks.

I would guess you have more than constants “spread over a couple of  
models”! If  
your design is indeed not clear and DRY, you ought to work to put each  
operation  
into the model where it belongs.

The same with constants; each must have a model that needs it most. Put  
it  
inside that model, and access it with Model:: notation.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 6, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/3 "2008-07-06T23:20:37Z")

</div>

Save The Vinyls wrote:

> Hello, I got a lot of Ruby constants spread over a couple of models.  
> Is it possible to throw them all into environment.rb instead? I’d  
> really enjoy having things in one place. Thanks.

Think it depends on what those constants are…

if they are environment specific stuff like

SITE\_LOCKOUT\_TIME = 15.minutes

which you may want to change to 30.seconds in development,  
then I’d probably put the default in environment.rb  
and do a

SITE\_LOCKOUT\_TIME = 15.minutes unless defined?(SITE\_LOCKOUT\_TIME)

then, you can override it for the development environment in in  
environments/development.rb

however,  
if it’s something that isn’t global, and isnt environment specific,  
then you should keep it in the relevant model.

eg.

class User  
LOCKOUT\_TIME = 15.minutes

def is\_locked\_out?  
self.locked\_at && self.locked\_at \> self.class::LOCKOUT\_TIME.ago  
end  
end

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 7, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/4 "2008-07-07T12:21:21Z")

</div>

say for people.rb:

MAX\_EMAIL = MAX\_PASSWORD = SMALL\_STRING\_LENGTH

MAX\_NAME = SMALL\_STRING\_LENGTH

MAX\_NICKNAME = 15

EMAIL\_REGEX = /\A[A-Z0-9.\_%-]+@([A-Z0-9-]+.)+[A-Z]{2,4}\z/i

DESCRIPTION\_LENGTH = 2000

TRASH\_TIME\_AGO = 1.month.ago

SEARCH\_LIMIT = 20

SEARCH\_PER\_PAGE = 8

MESSAGES\_PER\_PAGE = 5

NUM\_RECENT\_MESSAGES = 4

NUM\_WALL\_COMMENTS = 10

NUM\_RECENT = 8

FEED\_SIZE = 10

TIME\_AGO\_FOR\_MOSTLY\_ACTIVE = 1.month.ago

On Jul 6, 11:20 pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 7, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/5 "2008-07-07T12:45:24Z")

</div>

say for people.rb:

MAX\_EMAIL = MAX\_PASSWORD = SMALL\_STRING\_LENGTH

MAX\_NAME = SMALL\_STRING\_LENGTH

MAX\_NICKNAME = 15

EMAIL\_REGEX = /\A[A-Z0-9.\_%-]+@([A-Z0-9-]+.)+[A-Z]{2,4}\z/i

DESCRIPTION\_LENGTH = 2000

TRASH\_TIME\_AGO = 1.month.ago

SEARCH\_LIMIT = 20

SEARCH\_PER\_PAGE = 8

MESSAGES\_PER\_PAGE = 5

NUM\_RECENT\_MESSAGES = 4

NUM\_WALL\_COMMENTS = 10

NUM\_RECENT = 8

FEED\_SIZE = 10

TIME\_AGO\_FOR\_MOSTLY\_ACTIVE = 1.month.ago

On Jul 6, 11:20 pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 7, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/6 "2008-07-07T19:23:44Z")

</div>

thanks a lot for the response guys. i no longer feel this is  
necessary, i’ll keep the constants where they are.

On Jul 7, 1:23Â pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Save\_The\_Vinyls](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Save\_The\_Vinyls](https://www.ruby-forum.com/u/Save_The_Vinyls)\
**Post date:** [July 7, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/can-i-put-all-my-ruby-constants-in-environment-rb/142321/7 "2008-07-07T13:23:34Z")

</div>

Save The Vinyls wrote:

> say for people.rb:
> 
> MAX\_EMAIL = MAX\_PASSWORD = SMALL\_STRING\_LENGTH  
> EMAIL\_REGEX = /\A[A-Z0-9.\_%-]+@([A-Z0-9-]+.)+[A-Z]{2,4}\z/i  
> DESCRIPTION\_LENGTH = 2000  
> TRASH\_TIME\_AGO = 1.month.ago  
> SEARCH\_LIMIT = 20  
> SEARCH\_PER\_PAGE = 8  
> MESSAGES\_PER\_PAGE = 5  
> NUM\_RECENT\_MESSAGES = 4  
> NUM\_WALL\_COMMENTS = 10  
> NUM\_RECENT = 8  
> FEED\_SIZE = 10  
> TIME\_AGO\_FOR\_MOSTLY\_ACTIVE = 1.month.ago
> 
> On Jul 6, 11:20ï¿½pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

depends if you need to reuse this stuff or not.  
think it’s probably unecessary to freeze some of these as constants.

would probably rewrite it

class People  
validates\_length\_of :email, :password, :name, :maximum =\>  
SMALL\_STRING\_LENGTH  
validates\_length\_of :nickname, :maximum =\> 15  
validates\_format\_of :email,  
/\A[A-Z0-9.\_%-]+@([A-Z0-9-]+.)+[A-Z]{2,4}\z/i  
validates\_length\_of :description, :maximum =\> 2000

# note: TRASH\_TIME\_AGO = 1.month.ago will break

# as the constant is set at class load time

def self.to\_trash(time=1.month)  
self.find(:all, [“created\_at \< ?”, time.ago])  
end

named\_scope :mostly\_active, lambda{:conditions =\> [“updated\_at \> ?”,  
1.month.ago]}  
end

the stuff about number of messages to display and such  
are probably view variables,  
and you may want override with params…

hope that helps.

[http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs](http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs)
