3 ms·
Doesn't mention how @@ is evil. @@ are not your usual class variables. @@ should be avoided in most programs.
by denysonique 13y ago
Doesn't mention how @@ is evil. @@ are not your usual class variables. @@ should be avoided in most programs.
- VeejayRampay 13y agoYep, as Martin Fowler nicely points out in http://martinfowler.com/bliki/ClassInstanceVariable.html http://martinfowler.com/bliki/ClassInstanceVariable.html "A class variable is shared across all instances of a class, which includes all subclasses". Can be surprising when using inheritance.
- meowface 13y agoWell, that behavior is not surprising or anything. Many other languages have some kind of concept of a class instance variable (such as Python).
- valisystem 13y agoYou're right saying that it's the proper behavior for such a semantic. But the problem is, @@ notation can make you forget you're not manipulating something that might be shared and re-assigning an ancestor class variable accidentally, messing up its behavior. And while it's common to check for method overrides, class variable overrides is not something that is checked very frequently. It is the kind of bug that can be very long to figure out, since when you make this mistake, it's very likely because you forgot to think about this behavior.
- jwilliams 13y agoThis guide is specifically about the internals of and "hacking" the Ruby MRI -- it's not about hacking Ruby in general. Unless there is something specific about the MRI implementation and class variables?