We can create class variables by starting a variable name with two
@ symbols. Class variables are attached to entire classes, not just instances of classes, like so:
class MyClass @@class_variable end
Because there’s only one copy of a class variable shared by all instances of a class, we can use them to pull off some cool Ruby tricks. For example, we can use a class variable to keep track of the number of instances of that class we’ve created. Let’s do that now!
Let’s do three things:
- Create a class variable,
@@people_count, on line 3, and set it equal to 0.
@@people_counton line 8.
@@people_counton line 13 so that our
putson line 20 will print out the number of people created so far.