Equivalent of Java static code block in Ruby?

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

···

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

Hi --

···

On Wed, 12 Apr 2006, Wes Gamble wrote:

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?

How about:

   class MyClass
     # code here
   end

?

David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" coming in PDF April 15, and in paper May 5!

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

···

On 4/11/06, Wes Gamble <weyus@att.net> wrote:

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

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

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

Wes Gamble wrote:

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

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

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 Collins wrote:

···

Wes Gamble 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

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

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.

···

On 4/11/06, Wes Gamble <weyus@att.net> wrote:

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

Wes Gamble 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

Simon Kröger wrote:

Wes Gamble 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
  

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 Desmarais

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

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

Martin

Matthew,

I think that you are correct.

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.

Regards,
Matthew Desmarais

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

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?

Just a thought.

David Vallner