--
Wolfgang Nádasi-Donner
"Lionel Thiry" <> schrieb im Newsbeitrag
news:423473cb$0$28063$...
> >>>>>>Example >>>>>
> >
> > class Mytest
> > @@n_of_Mytest = 0
> > def initialize
> > @@n_of_Mytest += 1
> > end
> > def Mytest.n_of_Mytest
> > @@n_of_Mytest
> > end
> > end
> >
> > puts Mytest.n_of_Mytest
> > a = Mytest.new
> > puts Mytest.n_of_Mytest
> > b = Mytest.new
> > c = Mytest.new
> > puts Mytest.n_of_Mytest
> > d = Mytest.new
> > e = Mytest.new
> > f = Mytest.new
> > puts Mytest.n_of_Mytest
> >
> >>>>>>Output >>>>>
> >
> > 0
> > 1
> > 3
> > 6
> >
> >>>>>>End of Example >>>>>
> >
> >
> > O.K.?
> >
> >
>
> I've tested your code and I've been surprised that it actually worked. If
I
> correctly understand the mechanism, it's like the class object and its
instances
> are able to access class variables through '@@'. Undubitably, I know now
why I
> couldn't get along with that feature.
>
> Honestly, I largely prefer to think of classes as objects and use the
class
> instance variables when I want to share variables between instances. Using
'@@'
> doesn't seem reliable for me, as IMHO it violates some important OO
principles,
> the kind that if not followed leads to very embarrassing problems.
>
> Thanks for your help,
> Lionel Thiry
I think "@@" was based on Perl, where combinations of special characters
have (very very) special meanings.
The problem would be solved easily (I hope) with some kind of syntax like:
class Mytest
@@n_of_Mytest = 0
def initialize
Mytest.@@n_of_Mytest += 1
end
def Mytest.n_of_Mytest
@@n_of_Mytest
end
end
.... if the "@@"-notation will be stable (should be somehow, because all
books for beginners will refer to).