Is there a notion of a class level "static" code block in Ruby that
would get executed whenever the class is loaded? Basically the
equivalent of Java's "static {...}" construct?
I don't necessarily want to put this in an initialize method because
it's just loading up static data from configuration files.
Or do I have to put the code into an initialize method in my class and
then guard it so that it's only executed once?
Is there a notion of a class level "static" code block in Ruby that
would get executed whenever the class is loaded? Basically the
equivalent of Java's "static {...}" construct?
I don't necessarily want to put this in an initialize method because
it's just loading up static data from configuration files.
Or do I have to put the code into an initialize method in my class and
then guard it so that it's only executed once?
Part I
In order to execute something when the class is loaded you just put it
there
The attr_* methods are a good example, they are executed when the class
statement is executed and their receiver is
of course the class itself.
So that would be like
class Foo
puts "#{self}Bar" @bar = "foo"
...
Part II
def statements are of course defining instance methods so what if we want
static methods (well there is no such thing), class methods
there are at least 3 options
...
class << self
def static
or
def self.static
or
def Foo.static # not sure if it works is a maintainence nightmare anyway
!!!
Is there a notion of a class level "static" code block in Ruby that
would get executed whenever the class is loaded? Basically the
equivalent of Java's "static {...}" construct?
I don't necessarily want to put this in an initialize method because
it's just loading up static data from configuration files.
Or do I have to put the code into an initialize method in my class and
then guard it so that it's only executed once?
Is there a notion of a class level "static" code block in Ruby that would get executed whenever the class is loaded? Basically the equivalent of Java's "static {...}" construct?
I don't necessarily want to put this in an initialize method because it's just loading up static data from configuration files.
Or do I have to put the code into an initialize method in my class and then guard it so that it's only executed once?
Thanks,
Wes
Code placed within the class definition, but not in methods:
irb(main):001:0> class A
irb(main):002:1> puts "Hello!"
irb(main):003:1> end
Hello!
=> nil
Note how Hello! is output as soon as the class is parsed. I think this is what you want...?
That's fine but I only want it executed ONCE the first time the class is loaded.
I have a Rails application that creates a new INSTANCE of a controller class every time a certain URL is requested.
All of the code that isn't in methods is being executed every time the instance is created.
Maybe i don't understand but i think this is plain wrong:
class A
puts "Hello"
end
100.times do
a = A.new
end
#=> Hello
cheers
Simon
I think you need to find out why the class is being loaded multiple times. I may be completely off base here, but don't you load your entire app every time that you get a new request in Rails development mode? That would explain the results that you're seeing.
My irb testing shows me what everyone has said is true.
Thanks for the reminder.
Wes
Matthew Desmarais wrote:
···
Simon Kröger wrote:
end
#=> Hello
cheers
Simon
I think you need to find out why the class is being loaded multiple
times. I may be completely off base here, but don't you load your
entire app every time that you get a new request in Rails development
mode? That would explain the results that you're seeing.
Dňa Utorok 11. Apríl 2006 23:56 Matthew Desmarais napísal:
I think you need to find out why the class is being loaded multiple
times. I may be completely off base here, but don't you load your
entire app every time that you get a new request in Rails development
mode? That would explain the results that you're seeing.
Does it behave that way for BEGIN {} and END {} blocks too?
Religious sidenote: you should not use "static" code to alter application
state, lest classloading (-parsing) order static data clobbering grues eat
your face sooner than you think.
Extract the configuration handling into a singleton initialized from them
files perhaps?