Forum: Ruby Equivalent of Java static code block in Ruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-11 22:52
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-11 23:03
(Received via mailing list)
Hi --

On Wed, 12 Apr 2006, Wes Gamble wrote:

> 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!
http://www.manning.com/black
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2006-04-11 23:03
(Received via mailing list)
On 4/11/06, Wes Gamble <weyus@att.net> 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 ;)
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
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (Guest)
on 2006-04-11 23:06
(Received via mailing list)
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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-11 23:17
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
8cfdf47d27893fbd55d6b9565c34fbe4?d=identicon&s=25 Bruce Woodward (Guest)
on 2006-04-11 23:40
(Received via mailing list)
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,

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.
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 Simon Kröger (Guest)
on 2006-04-11 23:49
(Received via mailing list)
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
0817571d150afead454f4220007042fe?d=identicon&s=25 Matthew Desmarais (Guest)
on 2006-04-11 23:58
(Received via mailing list)
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
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-12 00:02
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
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-04-12 02:34
(Received via mailing list)
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
D2ce3fc131413e4456054ea24efcca0e?d=identicon&s=25 unknown (Guest)
on 2006-04-13 11:36
(Received via mailing list)
> #=> Hello
>

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

Martin
This topic is locked and can not be replied to.