# How/where to declare a global data structure?

**URL:** <https://www.ruby-forum.com/t/how-where-to-declare-a-global-data-structure/130965>\
**Category:** Rails\
**Created:** [March 4, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/how-where-to-declare-a-global-data-structure/130965 "2008-03-04T20:39:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 4, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/how-where-to-declare-a-global-data-structure/130965/1 "2008-03-04T20:39:20Z")

</div>

I’d like to read a custom config file into a hash at rails startup and  
have access to this hash from my controller actions. I wrote a sub to  
create the hash in my application controller (wrong place?) but I’m  
having trouble accessing the hash from my actions. I’m probably doing  
something silly as I’m fairly new to RoR.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 4, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/how-where-to-declare-a-global-data-structure/130965/2 "2008-03-04T20:49:11Z")

</div>

Ed wrote:

> I’d like to read a custom config file into a hash at rails startup and  
> have access to this hash from my controller actions. I wrote a sub to  
> create the hash in my application controller (wrong place?) but I’m  
> having trouble accessing the hash from my actions. I’m probably doing  
> something silly as I’m fairly new to RoR.

The application controller seems the right place to put the method to  
read in the data. For it to be available to all controllers in their  
methods, you probably want to put it into a class variable…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 5, 2008, 4:31am UTC](https://www.ruby-forum.com/t/how-where-to-declare-a-global-data-structure/130965/3 "2008-03-05T04:31:45Z")

</div>

2008/3/4, Ed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’d like to read a custom config file into a hash at rails startup and  
> have access to this hash from my controller actions. I wrote a sub to  
> create the hash in my application controller (wrong place?) but I’m  
> having trouble accessing the hash from my actions. I’m probably doing  
> something silly as I’m fairly new to RoR.

Assuming it’s a yaml file, completely untested:

```
require "yaml"

class ApplicationController < ActionController::Base

  MY_CONFIG = YAML.load(File.read(File.join(RAILS_ROOT, "config",

```

“my\_config.yml”)))

```
end

```

Now you can simply access MY\_CONFIG from all your controllers.
