# How to create an object in environment.rb and use it to shar

**URL:** <https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332>\
**Category:** Rails\
**Created:** [November 19, 2005, 12:07am UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332 "2005-11-19T00:07:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 19, 2005, 12:07am UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332/1 "2005-11-19T00:07:23Z")

</div>

Hi,

I was using a global ($) variable for this, but it seems global  
variables remain loaded all the time that webrick is running, and I need  
a fresh object for each HTTP request.

So I was trying to use the request.env array to add my own data there,  
but it seems that that object isn’t created in environment.rb, so I  
cannot use that approach. I need to create an object with some default  
values on environment.rb, and then each controller can override any of  
its attributes.

Could you please help me out?

- Ivan V.

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 19, 2005, 1:39am UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332/2 "2005-11-19T01:39:56Z")

</div>

ivanvega wrote:

> Hi,
> 
> I was using a global ($) variable for this, but it seems global  
> variables remain loaded all the time that webrick is running, and I need  
> a fresh object for each HTTP request.
> 
> So I was trying to use the request.env array to add my own data there,  
> but it seems that that object isn’t created in environment.rb, so I  
> cannot use that approach. I need to create an object with some default  
> values on environment.rb, and then each controller can override any of  
> its attributes.
> 
> Could you please help me out?
> 
> - Ivan V.

Hey Ivan,

As newby-on-rails, I don’t know if this suggestion will be of any help  
to you, but for what it’s worth…

Because I will soon need to figure out how to do what is commonly  
referred to as an “application variable” in other languages… (i.e. a  
variable that all open sessions have access to).

I started doing some research & ran across something called “PStore”.

It’s described as a way of storing session information in a file in the  
tmp dir… I know this is not what you are trying to do, but since it’s  
possible, I assume, to read & write to this file from anywhere in the  
app (I think read & write locking is built-in), perhaps you could use it  
for your purposes?

I haven’t figured out how to use it yet, but here’s a wiki entry that  
mentions it as well as other approaches;

[http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/](http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore/)

I hope you get a more useful answer from someone who knows far more than  
I…

Randy

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 19, 2005, 1:59am UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332/3 "2005-11-19T01:59:06Z")

</div>

Hey,

Thanks for the input. Unfortunately, as you point out, that’s not what  
I’m trying to do, because I need a clean object variable each time a  
page is requested, so a session/file wouldn’t work (well it could, but  
it would be very complicated and way overkill). But I’m thinking that  
might be of use for another area of my project!

I believe this is only an issue with webrick, because the application is  
running the whole time, so I suppose Apache wouldn’t have this problem  
because it runs the script from scratch for each request, I think…

Let’s hope someone can help me out!

Ivan V.

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 21, 2005, 7:13pm UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332/4 "2005-11-21T19:13:42Z")

</div>

Ezra Z. wrote:

> attr\_writer :foo  
> think this is what you are trying to do. Let me know if you need more  
> help.
> 
> Cheers-
> 
> -Ezra

Thanks a lot Ezra, that’s just what I was looking for.

Ivan V.

---

<div class="post-metadata">

**Author:** ![ivanvega](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ivanvega](https://www.ruby-forum.com/u/ivanvega)\
**Post date:** [November 19, 2005, 2:14am UTC](https://www.ruby-forum.com/t/how-to-create-an-object-in-environment-rb-and-use-it-to-shar/48332/5 "2005-11-19T02:14:10Z")

</div>

Ivan-

```
You can place a before filter in your application.rb that gets

```

initialized to whatever you want on each request. Like htis:

class ApplicationController \< ActionController::Base

```
before_filter :setup_foo

attr_reader :foo
attr_writer :foo
def setup_foo
   @foo = {:a => 'foo, :b => 'bar', :c => 'qux'}
end

```

end

```
Now @foo will be available to all your controllers and views. And

```

the attr\_reader and attr\_writer will set it up so your controllers  
can modify foo per request. But @foo will be reset on each request. I  
think this is what you are trying to do. Let me know if you need more  
help.

Cheers-

-Ezra

On Nov 18, 2005, at 4:57 PM, Iván Vega R. wrote:

> have this problem because it runs the script from scratch for each
> 
> > > values on environment.rb, and then each controller can override  
> > > As newby-on-rails, I don’t know if this suggestion will be of any  
> > > but since it’s possible, I assume, to read & write to this file
> > 
> > Randy
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
