[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class Variables and Singelton Mix-Ins

Sebastia

1/25/2007 12:52:00 AM

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?

3 Answers

Sebastia

1/25/2007 1:00:00 AM

0

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


Gary Wright

1/25/2007 5:17:00 AM

0


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 Wright




Verno Miller

1/25/2007 12:57:00 PM

0

> 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-me...

Cheers,
verno

--
Posted via http://www.ruby-....