Class Variables and Singelton Mix-Ins

I'm trying to find a way to declare class variables in a module that can be mixed into class methods and retain scope for only that particular class instead of being defacto module-variables.

Here's a simplified example (albeit somewhat silly) of what i'm trying to do:

module Foo
  module Bar
      #...some instance methods

    def self.included(klass)
      klass.extend(BarClassMethods)
    end

  end

  module BarClassMethods

    def greeting
      @@greeting ||= self.determine_greeting
    end

    def determine_greeting
      hello
    end

  end
end

class A
  include Foo::Bar

     def self.hello
       "hello from A"
     end

end

class B
  include Foo::Bar

  def self.hello
     "hello from B"
   end

end

puts A.greeting
#=> 'hello from A'

puts B.greeting
#=> 'hello from A'

The result i would hope for is "hello from A" and "hello from B" respectively. But since @@greeting is really scoped module-wide it is only intialized once. How can i force the class variable to pertain only to the singelton class that mixes it in?

uuh-ooh. i guess i could use an instance variable for the Singleton class instead :slight_smile:

    def greeting
      @greeting ||= self.determine_greeting
    end

It seems to have the exact same effect than a class variable defined in the class. Is this correct?

···

On 2007-01-24 18:51:46 -0600, Sebastian <shfriedrich@gmail.com> said:

I'm trying to find a way to declare class variables in a module that can be mixed into class methods and retain scope for only that particular class instead of being defacto module-variables.

Here's a simplified example (albeit somewhat silly) of what i'm trying to do:

module Foo
  module Bar
      #...some instance methods

    def self.included(klass)
      klass.extend(BarClassMethods)
    end

  end

  module BarClassMethods

    def greeting
      @@greeting ||= self.determine_greeting
    end

    def determine_greeting
      hello
    end

  end
end

class A
  include Foo::Bar

     def self.hello
       "hello from A"
     end

end

class B
  include Foo::Bar

  def self.hello
     "hello from B"
   end

end

puts A.greeting
#=> 'hello from A'

puts B.greeting
#=> 'hello from A'

The result i would hope for is "hello from A" and "hello from B" respectively. But since @@greeting is really scoped module-wide it is only intialized once. How can i force the class variable to pertain only to the singelton class that mixes it in?

uuh-ooh. i guess i could use an instance variable for the Singleton class instead :slight_smile:

    def greeting
      @greeting ||= self.determine_greeting
    end

That isn't an instance variable for the singleton class. It is an
instance variable associated with the class itself. The singleton
class for an object acts as a container for method definitions but
it is the object itself that has the instance variables.

It seems to have the exact same effect than a class variable defined in the class. Is this correct?

There are various differences but the biggest is that a class
instance variable, just like all instance variables, is only
accessible via the individual class object and not through any
subclasses as would be the case with a class variable.

Most Ruby veterans seem to shun Ruby class variables in favor of
class instance variables. Despite the name, Ruby class variables
are not like class variables in other object oriented languages.
Almost always a 'class instance variable' is a better choice.

Gary Wright

···

On Jan 24, 2007, at 8:05 PM, Sebastian wrote:

Posted by Sebastian (Guest) on 25.01.2007 01:56

I'm trying to find a way to declare class variables in a module that
can be mixed into class methods and retain scope for only that
particular class instead of being defacto module-variables.

Maybe check out:
http://redcorundum.blogspot.com/2006/06/mixing-in-class-methods.html

Cheers,
verno

···

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