# Class level "static" code in controller object not working

**URL:** <https://www.ruby-forum.com/t/class-level-static-code-in-controller-object-not-working/93846>\
**Category:** Rails\
**Created:** [April 11, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/class-level-static-code-in-controller-object-not-working/93846 "2006-04-11T23:33:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/class-level-static-code-in-controller-object-not-working/93846/1 "2006-04-11T23:33:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 12, 2006, 12:03am UTC](https://www.ruby-forum.com/t/class-level-static-code-in-controller-object-not-working/93846/2 "2006-04-12T00:03:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 12, 2006, 1:42am UTC](https://www.ruby-forum.com/t/class-level-static-code-in-controller-object-not-working/93846/3 "2006-04-12T01:42:19Z")

</div>

Yep, that’s exactly it.

Pete Y.  
[http://9cays.com](http://9cays.com)
