# probable initialization bug gets introduced in this code
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
  Maybe warnings to the instance variable assignments in the class body might solve the problem very nicely without modifying language syntax.
- matz.
@@_@classInstanceVariable "@@ associated with class", and _also_ <br> " _@ associated with an instance: an instance of a class in this case"
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 ;)
Maybe warnings to the instance variable assignments in the class body might solve the problem very nicely without modifying language syntax.
- matz.
@@_@classInstanceVariable "@@ associated with class", and _also_ <br> " _@ associated with an instance: an instance of a class in this case"
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 ;)
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog