rubyx
rubyx copied to clipboard
Class Variables
I just did class instance variables and will leave class variables for a later (much later?) time. If unclear on the difference, this is a nice explanation https://stackoverflow.com/questions/3802540/difference-between-class-variables-and-class-instance-variables
I don't use class variables and feel they have weird scoping rules, no data hiding and a possibly a design mistake. To be more precise, when A < B and B < C and A has a class variable, C can change it and B will see that change. This is weird. Also the @@class_variable is available inside normal object methods. Unlike the cleaner @class_instance_variable only being available in class methods. Breaks data hiding, more weird. And when you access an undefined instance variable, be it in an object or a class, you just get nil, but if you do it with a class variable, it raises an error. More weirdness, which potentially makes initialization a tricky and important thing.
I will leave this for someone to pick up (as for compliance it needs to be done), and outline how, roughly:
- a separate variable that holds all class variables has to be created in the class, say class_variables
- class variable access has to be mediated to this variable as a lookup and a raise generated for fails
- access in derived classes needs to walk the hierarchy for the access (above)
Probably the lookup / access is so complicated that it needs to be done through methods, in which case temporary variables will be needed (as for any send)