Skip to Content
Learn
Object-Oriented Programming I
Twice the @, Twice as Classy

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.
Folder Icon

Take this course for free

Already have an account?