Loading file at start?

Hello,

A simple question … I would like to load a file at the initialization
of my app, to populate and share an Hash in all my app.

Where to do this kind of initialization ?

I did something like @@myVar=File.open … at the end of the file
config/environment.rb , correct ?

thanks
arnaud

Arnaud G. wrote:

Hello,

A simple question … I would like to load a file at the initialization
of my app, to populate and share an Hash in all my app.

Where to do this kind of initialization ?

I did something like @@myVar=File.open … at the end of the file
config/environment.rb , correct ?

thanks
arnaud

Define a little class for and stick in /lib it like so

#/lib/my_loader_thing.rb
class MyLoaderThing
cattr_accessor :data
def self.load
@@data = {
:foo => ‘bar’
:baz => ‘bingo’
}
end
end

Now in your environment.rb, at the bottom:

require ‘my_loader_thing’
MyLoaderThing.load

Now throughout your app you can use that class attribute accessor like
so:

MyLoaderThing.data[:foo] #=> “bar”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs