From: Dan Doel Date: 2003-08-22T10:42:02+09:00 Subject: Re: Class variables - a surprising result dblack@superlink.net wrote: >No, he's referring to this: > > class A > end > > class B < A > @@x = 1 > end > > class A > @@x = 2 > end > > class B > p @@x # prints 1, not 2 -- A and B are not sharing @@x > end > > Heh, oh, that is rather bad. >I *knew* there was some reason I didn't want to learn Java.... :-) > > It's only more Java-esque because the "variables" are Class.var_name Instead of: @@var_name You could invent symbols for all these idioms ($@var_name, @@@var_name, or whatever) but that'd require changing the interpreter. >>Code below. >> >> > >Still scanning it, but one quick comment: I notice you don't use >public/private/protected. Are there places where that might give you >the same kind of functionality? > Realistically, you would want to use public/private/protected. If you want to keep in the spirit of Ruby, the code should probably be modified so that all these accessors are private. If you're more of a Java person, I suppose you could modify it so that you could specify the access control level. I left that part out because it's not that hard to add, it's a bit of a side issue, and I don't really know how everyone would want it to work (and I'm not enough of a language theorist to make that decision for everyone :) ). In my first attempts, I coded up a system where if you used the reader, it would look in the current class, and if that wasn't available, it would look in superclasses, but if you used the writer it would only affect the current class. However, that seemed too weird for me. I think the two I gave earlier are probably the most understandable. I'm not sure what you meant by giving "the same kind of functionality." They set method access control, and I'm not sure what that has to do with making class/static/class-instance varibles. But I could be missing something. Cheers. - Dan