Class Variables and Singelton Mix-Ins

S

Sebastian

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?
 
S

Sebastian

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?
 
G

gwtmp01

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top