14.2. Variables
Ruby supports a couple different types of variables, instance variables and class variables. Instead of making you guess whether their names actually mean what they say, I'll just come out and say it. The names mean what they say. Instance variables are created for each instance of the class. With class variables, on the other hand, all instances of the class share one variable. Although instance variables are common, class variables are somewhat less so. This does not mean that they aren't as useful; in fact, many times there is simply no substitute for a class variable.
The only question concerning variables is how to distinguish between instance variables and class variables. Are there little signs hanging off them that say "instance variable" and "class variable"? In a word, yes.
Instance variables and class variables are distinguished by the prefix. Instance variables are prefixed by a single @, whereas class variables are prefixed by two @. So @Bob is an instance variable, and @@Paul is a class variable.
Now that we have someplace to put our information, let's do something to it.
|