From: Sebastian Date: 2007-01-25T10:05:10+09:00 Subject: Re: Class Variables and Singelton Mix-Ins uuh-ooh. i guess i could use an instance variable for the Singleton class instead :) > 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 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?