[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Class instance variables, class variables and constants

Dominik Bathon

2/20/2006 4:10:00 PM

On Mon, 20 Feb 2006 15:22:04 +0100, Tony Mobily <merc@mobily.com> wrote:

> Hello,
>
> Thanks to the code in attach, I've figured out what is now completely
> obvious to me:
>
> CLASS VARIABLE (@@something)
> - Inherited by subclasses
> - SAME ALLOCATION: If it's changed in Child, then it will change in
> Parent
>
> CONSTANTS (Something)
> - Inherited by subclasses
> - PRIVATE ALLOCATION: If it's changed in Child, it will NOT change in
> parent
>
> CLASS INSTANCE VARIABLES (@something)
> - NOT Inherited by subclasses. They will simply be nil. Scope = Class
> - DIFFERENT ALLOCATION: If it's changed in Child, it will NOT change in
> parent

But there is more fun when singleton classes get involved:

class A
@@var = :A
C = :CA
end

class B
@@var = :B
C = :CB

a = A.new
class ::A
p [@@var, C] # => [:A, :CA]
def foo
[@@var, C]
end
end
def a.bar
[@@var, C]
end
class << a
p [@@var, C] # => [:B, :CB]
def baz
[@@var, C]
end
end
p a.foo # => [:A, :CA]
p a.bar # => [:B, :CB]
p a.baz # => [:B, :CB]
p [@@var, ::A::C, C, class << a;C;end] # => [:B, :CA, :CB, :CB]
class << a
@@var = :ASing # this changes B's @@var [*]
C = :CASing # this creates a new C for a's sing. class
end
p [@@var, ::A::C, C, class << a;C;end] # => [:ASing, :CA, :CB, :CASing]
p a.foo # => [:A, :CA]
p a.bar # => [:ASing, :CB]
p a.baz # => [:ASing, :CASing]
end

Some points:
- singleton classes don't have their own class variables, but they can
have their own constants
- constant and class var lookup use the lexical scope, so it depends
"where" you do something (that's why bar's C and baz's C are different)
- (lexical) singleton-class-scopes are skipped when accessing class vars
(that's why [*] changes B's @@var and not A's @@var)

I hope that helps and doesn't totally confuse you ;-)

Dominik


5 Answers

Tony Mobily

2/20/2006 10:35:00 PM

0

Hi,

> Some points:
> - singleton classes don't have their own class variables, but they
> can have their own constants
> - constant and class var lookup use the lexical scope, so it
> depends "where" you do something (that's why bar's C and baz's C
> are different)
> - (lexical) singleton-class-scopes are skipped when accessing class
> vars (that's why [*] changes B's @@var and not A's @@var)
>
> I hope that helps and doesn't totally confuse you ;-)

OK. One hour later, this is became clear...
I'd write the points above in a different order:

- singleton classes can have their own constants

- singleton classes don't have their own class variables. (Lexical)
singleton-class-scopes are skipped when accessing class vars

- constant and class variable lookup use the lexical scope, so it
depends "where" you do something

Now... I do have a question. On one hand, I really, really want to
know all these fine details. That's just my nature.
However, in this case, I must ask: is this going to be *useful* at
any point in time?
When would you want to set a constant for a singleton class?

The rest makes perfect sense to me: a singleton class could
realistically need access a class variable. And I realise that I need
to know that constant and class variable lookup use the lexical
scope. But... singleton constants...?

Just curious!

Merc.


David Vallner

2/21/2006 2:37:00 AM

0

Dna Pondelok 20 Február 2006 23:35 Tony Mobily napísal:
> When would you want to set a constant for a singleton class?
>

Hardly ever? It's not like you need more instances to always get the same
value.

David Vallner


dblack

2/21/2006 3:06:00 AM

0

I&R ~ GB

5/6/2010 10:41:00 AM

0

GM wrote:
> On 5 May, 21:11, soupdragon <m...@privacy.com> wrote:
>> I&R ~ GB <infoT...@OUTiniref.org> wrote innews:84dml2Fs0nU1@mid.uni-berlin.de:
>>
>>> If you are still undecided about which party or parties should form
>>> the next government and about which candidate to vote, or if you are
>>> just one of many people who seriously worry about the condition of our
>>> democracy then take a look at our Guide athttp://www.i...
>> Glad to see you've finally seen sense and abandoned your silly 'spoil
>> your paper' campaign.
>
> I agree - well done guys.
>
> GMc

Maybe the best way to vote for better democracy would be to choose the
Scottish National Party.

They recently replied to this question as follows:
8. Direct Democracy - does the SNP have policy on increasing direct
democracy?

"The SNP would like to see direct democracy initiatives that would see
the sharing of power with people, giving them real power and a direct
say over the most important issues affecting their communities. This
would include the triggering of referendums on any national or local
issue once a requisite percentage of the electorate had signed a
petition on the matter, as undertaken in the likes of Switzerland, the
US and New Zealand.

We also support a new Petitions Committee in the House of Commons, much
like that of the Scottish Parliament, which encourages active
involvement in the proceedings of Parliament.

SOURCE Unlock Democracy and the www.votefordemocracy.org.uk
---------------------
Sorry that we forgot to mention this before election day ;-)

I&R ~ GB Citizens' Initiative and Referendum
Campaign for direct democracy in Britain

http://www.i... election campaign call
http://www.i...index.enter.html web site index

GM

5/6/2010 4:32:00 PM

0

On 6 May, 12:40, I&R ~ GB <infoT...@OUTiniref.org> wrote:
> GM wrote:
> > On 5 May, 21:11, soupdragon <m...@privacy.com> wrote:
> >> I&R ~ GB <infoT...@OUTiniref.org> wrote innews:84dml2Fs0nU1@mid.uni-berlin.de:
>
> >>> If you are still undecided about which party or parties should form
> >>> the next government and about which candidate to vote, or if you are
> >>> just one of many people who seriously worry about the condition of our
> >>> democracy then take a look at our Guide athttp://www.i...
> >> Glad to see you've finally seen sense and abandoned your silly 'spoil
> >> your paper' campaign.
>
> > I agree - well done guys.
>
> > GMc
>
> Maybe the best way to vote for better democracy would be to choose the
> Scottish National Party.
>
> They recently replied to this question as follows:
> 8. Direct Democracy - does the SNP have policy on increasing direct
> democracy?
>
> "The SNP would like to see direct democracy initiatives that would see
> the sharing of power with people, giving them real power and a direct
> say over the most important issues affecting their communities. This
> would include the triggering of referendums on any national or local
> issue once a requisite percentage of the electorate had signed a
> petition on the matter, as undertaken in the likes of Switzerland, the
> US and New Zealand.
>
> We also support a new Petitions Committee in the House of Commons, much
> like that of the Scottish Parliament, which encourages active
> involvement in the proceedings of Parliament.
>
> SOURCE Unlock Democracy and thewww.votefordemocracy.org.uk
> ---------------------
> Sorry that we forgot to mention this before election day ;-)
>
> I&R ~ GB Citizens' Initiative and Referendum
> Campaign for direct democracy in Britain
>
> http://www.i...election  campaign callhttp://www.i...index.enter.html web site index

The Scottish Parliament has had a petitions committee for years.

GMc