[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Singleton Class or Module Functions?
From:       Brian Candler <B.Candler () pobox ! com>
Date:       2004-10-21 15:43:26
Message-ID: 20041021154319.GA876 () uk ! tiscali ! com
[Download RAW message or body]

> module Foo
>	class << self
>		def initialize
>			puts "Never called"
>		end
>	end
> end

  module Foo
    puts "Called once when module loaded"
  end

But maybe you want to defer this until some later point in time. You could
always write

  module Foo
    def self.setup
      return if defined?(@initialized)
      @initialized = true
      puts "Doing it once"
    end
  end

  Foo.setup
  Foo.setup

but that's not thread-safe; if that's a concern then Singleton is going to
be better.

Regards,

Brian.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic