# Class Variables and Singelton Mix-Ins

**URL:** <https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314>\
**Category:** Ruby\
**Created:** [January 25, 2007, 1:56am UTC](https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314 "2007-01-25T01:56:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 25, 2007, 1:56am UTC](https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314/1 "2007-01-25T01:56:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 25, 2007, 2:05am UTC](https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314/2 "2007-01-25T02:05:18Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 25, 2007, 6:17am UTC](https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314/3 "2007-01-25T06:17:39Z")

</div>

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

> uuh-ooh. i guess i could use an instance variable for the Singleton  
> class instead 🙂
> 
> > ```
> > 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 W.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 25, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/class-variables-and-singelton-mix-ins/83314/4 "2007-01-25T13:57:13Z")

</div>

> 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](http://redcorundum.blogspot.com/2006/06/mixing-in-class-methods.html)

Cheers,  
verno
