Class level "static" code in controller object not working

All,

I have the following code that I put in my controller which I am using
to initialize some static data. Note that it is not contained with a
method of any kind.

puts “test”
config = YAML.load_file(“config/config.yml”) if config.nil?
Dir.mkdir(config[‘html_upload_folder’]) unless
File.exist?(config[‘html_upload_folder’])

I would like this code to only be executed ONCE when the controller
class is loaded but what I am seeing is that every time I invoke a URL
that is related to this controller I see “test” printed on the console.

Which would indicate to me that this code is being executed on each
instantiation of the controller object, not just when the controller
class is loaded.

In irb, the code that’s just in a class but not in a method does only
get executed when the class is loaded not when new instances are made.

Is the controller class being reloaded each time???

Wes

P. S. I am trying to get the equivalent of a Java “static” block here.

It occurs to me that in development mode, the controller class is
reloaded every time isn’t it, so does that explain it?

Thanks,
Wes

Wes G. wrote:

All,

I have the following code that I put in my controller which I am using
to initialize some static data. Note that it is not contained with a
method of any kind.

puts “test”
config = YAML.load_file(“config/config.yml”) if config.nil?
Dir.mkdir(config[‘html_upload_folder’]) unless
File.exist?(config[‘html_upload_folder’])

I would like this code to only be executed ONCE when the controller
class is loaded but what I am seeing is that every time I invoke a URL
that is related to this controller I see “test” printed on the console.

Which would indicate to me that this code is being executed on each
instantiation of the controller object, not just when the controller
class is loaded.

In irb, the code that’s just in a class but not in a method does only
get executed when the class is loaded not when new instances are made.

Is the controller class being reloaded each time???

Wes

P. S. I am trying to get the equivalent of a Java “static” block here.

Yep, that’s exactly it.

Pete Y.
http://9cays.com