Submitted by coreywangler (Wed Jan 07 16:01:21 UTC 2004)
class Foo
@a = 123 # (1) class instance var initialized def foo p @a # (2) 'normal' instance var accessed ...nil not 123 end
end
@@classVariable
* one value shared by all instances of the class * visible in instance methods * acts like a global variable for the class/module
@@_classInstanceVariable #<=-modified syntax
* instances of the class each get their own value to use * not visible in instance methods * could think of it as a "scratch variable" for use by class methods * think of underscore as meaning "internal use", "hidden"
@instanceVariable
* can be accessed by subclasses
@_restrictedInstanceVariable #<=-new syntax
* not accessible by subclasses * think of underscore as meaning "internal use", "hidden"
Note that (with the renaming of @classInstanceVar to @@_classInstanceVar) @variables cannot be initialized or used outside of instance methods, and should give an error message like "an @variable can only be used within instance methods" if such use is attempted.
class Foo
# not allowed - use a @@_classInstanceVariable here instead if # that is the intention # error message should say something like # "@variable must be initialized within an instance method" @variable = 0 #produce an error message # initialize a class variable, accessible within both # class methods and instance methods of the class @@exampleClassVariable = 880 # initialize a class instance variable, # not accessible by instance methods @@_exampleClassInstanceVariable = 770 # class method def Foo.setExampleClassVariable(value) @@exampleClassVariable = value end # another class method def Foo.setExampleClassInstanceVariable(value) @@_exampleClassInstanceVariable = value end def initialize # initialize an instance variable, accessible within # instance methods of class and subclasses @exampleInstanceVariable = 30 # initialize a "restricted instance variable", # not accessible by subclasses @_exampleRestrictedInstanceVariable = 20 # modify a class variable @@exampleClassVariable += 1 end
end
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
This RCR has been superseded by RCR 186.
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
In Ruby, classes are object, no exception. This is very important principle. Your proposal may hinder learning of this principle.
Maybe warnings to the instance variable assignments in the class body might solve the problem very nicely without modifying language syntax.
- matz.
Class instance variable is a special case
Class instance variables are a special case, because those can only be used in class methods and are not visible in instance methods.What I am proposing is a re-think on what @@ means...
"@@ is associated with class implementation".
"@ is associated with instance implementation."
I also thought about suggesting @@_@classInstanceVariable (instead of the original suggested @@_classInstanceVariable). This would make it clear that it is both
Maybe I should resubmit the RCR with that, as I like it better after considering your comment.
Another thing to consider is how much this feature of the language (class instance variables) is used. A lot of programmers would never touch such an animal, because it is an advanced concept used for meta-programming.
I am guessing that most usage would be of the plain @@classVariable, and you would rarely see @@_@classInstanceVariable. Such notation is a bit uglier, but ensures that the programmer gets exactly what they ask for (less surprise! -- I didn't mention POLS ...doh, wait I just did ;)