Equivalent of Java static code block in Ruby?


#1

All,

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


#2

Hi –

On Wed, 12 Apr 2006, Wes G. wrote:

then guard it so that it’s only executed once?
How about:

class MyClass
# code here
end

?

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” coming in PDF April 15, and in paper May 5!
http://www.manning.com/black


#3

On 4/11/06, Wes G. removed_email_address@domain.invalid wrote:

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


Posted via http://www.ruby-forum.com/.

Well unless I missunderstood this is an easy one

Part I
In order to execute something when the class is loaded you just put it
there :wink:
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
!!!

Hope that helps

Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

#4

Wes G. wrote:

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…?

-Justin


#5

On 4/11/06, Wes G. removed_email_address@domain.invalid wrote:

That’s fine but I only want it executed ONCE the first time the class is
loaded.

Wes,

I don’t about the first time that the class is loaded, but you get
handle
the first time that the class is instantiated.

class Foo
@@first_time = true
def initialize
if @@first_time
puts “perform initialization”
@@first_time = false
end
end
end

f = Foo.new
ff = Foo.new

cheers,
Bruce.


#6

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.

So do I have to guard my code to ensure that it’s only executed once?

Thanks,
Wes

Justin C. wrote:

Wes G. wrote:

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…?

-Justin


#7

Wes G. wrote:

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


#8

Matthew,

I think that you are correct.

My irb testing shows me what everyone has said is true.

Thanks for the reminder.

Wes

Matthew D. 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.

Regards,
Matthew D.


#9

DÅ?a Utorok 11. Apríl 2006 23:56 Matthew D. 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?

Just a thought.

David V.


#10

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.

Regards,
Matthew D.


#11

#=> Hello

No, that is what I would expect. Anything else is broken.

Martin