# How to initialize variables automatically on application bootstrap?

**URL:** <https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232>\
**Category:** Rails\
**Created:** [April 15, 2013, 12:26am UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232 "2013-04-15T00:26:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 12:26am UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/1 "2013-04-15T00:26:02Z")

</div>

How to set some variables on bootstap? I need a few variables to be  
initialized with default values every request. For example in ZF there  
is a class Bootstrap whose methods are called automatically every  
request. Is there something similar in Rails?

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 1:33am UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/2 "2013-04-15T01:33:05Z")

</div>

On Sun, Apr 14, 2013 at 5:26 PM, Wins L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to set some variables on bootstap? I need a few variables to be  
> initialized with default values every request. For example in ZF there  
> is a class Bootstrap whose methods are called automatically every  
> request. Is there something similar in Rails?

Do these need to be initialized before configuration? If not, you can  
put them in config/initializers/

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 1:36am UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/3 "2013-04-15T01:36:36Z")

</div>

On Sun, Apr 14, 2013 at 6:31 PM, tamouse mailing lists  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, Apr 14, 2013 at 5:26 PM, Wins L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > How to set some variables on bootstap? I need a few variables to be  
> > initialized with default values every request. For example in ZF there  
> > is a class Bootstrap whose methods are called automatically every  
> > request. Is there something similar in Rails?
> 
> Do these need to be initialized before configuration? If not, you can  
> put them in config/initializers/

Never mind, I mis-read what you wrote.

ZF I’m assuming is Zend Framework, aka, PHP? Which works nothing like  
Rails, btw, as you are essentially restarting the entire application  
with every request in PHP.

The Rails application is not brought up and shut down for every  
request. It lives in a long running process.

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 1:43am UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/4 "2013-04-15T01:43:01Z")

</div>

Maybe hardcoding them in a before\_filter on ApplicationController…

Which kind of variables are you talking about? Where do you plan to use  
them?

2013/4/14 tamouse mailing lists [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

–

Ricardo Franco Andrade

( Web | Desktop | Game ) Developer

email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
phone: +55 (86) 9436 0830  
linkedin: [http://www.linkedin.com/pub/ricardo-franco/61/2b0/857](http://www.linkedin.com/pub/ricardo-franco/61/2b0/857)  
skype: ricardo.krieg  
github: [ricardokrieg (Ricardo Andrade) · GitHub](https://github.com/ricardokrieg)  
twitter: [twitter.com/ricardokrieg](http://twitter.com/ricardokrieg)  
facebooK: [https://www.facebook.com/krieg.ricardo](https://www.facebook.com/krieg.ricardo)

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 2:10pm UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/5 "2013-04-15T14:10:45Z")

</div>

tamouse mailing lists wrote in post #1105626:

> On Sun, Apr 14, 2013 at 6:31 PM, tamouse mailing lists  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Never mind, I mis-read what you wrote.
> 
> ZF I’m assuming is Zend Framework, aka, PHP? Which works nothing like  
> Rails, btw, as you are essentially restarting the entire application  
> with every request in PHP.

Yes, I meant Zend Framework.

> The Rails application is not brought up and shut down for every  
> request. It lives in a long running process.

But how it can be? HTTP is a stateless protocol.

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/6 "2013-04-15T14:08:01Z")

</div>

Ricardo Franco wrote in post #1105627:

> Maybe hardcoding them in a before\_filter on ApplicationController…
> 
> Which kind of variables are you talking about? Where do you plan to use  
> them?

Yes, “before\_filter” works just fine. Yet do not understand the details  
how it works but it works 🙂

I want to have a predefined variable in every method of my controllers.  
It is @isAuth = false. I did like this in “application\_controller.rb”:

before\_filter :setAuth  
def setAuth  
@isAuth = false  
end

Thanks.

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 3:19pm UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/7 "2013-04-15T15:19:11Z")

</div>

On Mon, Apr 15, 2013 at 7:10 AM, Wins L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> tamouse mailing lists wrote in post #1105626:
> 
> > On Sun, Apr 14, 2013 at 6:31 PM, tamouse mailing lists  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > The Rails application is not brought up and shut down for every  
> > request. It lives in a long running process.
> 
> But how it can be? HTTP is a stateless protocol.

It is quite possible to build a statefull protocol on top of a stateless  
one.

---

<div class="post-metadata">

**Author:** ![zvooq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/65b543/32.png) [@zvooq](https://www.ruby-forum.com/u/zvooq)\
**Post date:** [April 15, 2013, 2:17pm UTC](https://www.ruby-forum.com/t/how-to-initialize-variables-automatically-on-application-bootstrap/228232/8 "2013-04-15T14:17:48Z")

</div>

On Apr 15, 2013, at 8:10 AM, Wins L. wrote:

> Yes, I meant Zend Framework.
> 
> > The Rails application is not brought up and shut down for every  
> > request. It lives in a long running process.
> 
> But how it can be? HTTP is a stateless protocol.

Right, but Rails implements a full application server within its  
long-running process, and maintains a session cookie to track individual  
users across multiple request loops. PHP is just beginning to be able to  
do this sort of thing, I think. Isn’t there a server in the latest beta  
or alpha?

Walter
