Learn

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!

Instructions

1.

Let’s do three things:

  • Create a class variable, @@people_count, on line 3, and set it equal to 0.
  • Increment @@people_count on line 8.
  • Return @@people_count on line 13 so that our puts on line 20 will print out the number of people created so far.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?