Wes Gamble
2006-Apr-11 21:33 UTC
[Rails] 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. -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Apr-11 22:03 UTC
[Rails] Re: class level "static" code in controller object not worki
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 Gamble 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.-- Posted via http://www.ruby-forum.com/.
Pete Yandell
2006-Apr-11 23:39 UTC
[Rails] Re: class level "static" code in controller object not worki
Yep, that''s exactly it. Pete Yandell http://9cays.com On 12/04/2006, at 8:03 AM, Wes Gamble wrote:> 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 Gamble 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. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Apparently Analagous Threads
- Apache/fastcgi setup can''t find custom config/*.yml file!
- Mongrel doesn''t start under Rails 2.0.2/Win XP
- Do module source code changes require reboot of WEBrick?
- Specify a controller action via attribute in INPUT tag?
- Best way to organize non-controller logic???