From the course: Complete Guide to Ruby

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Class attributes

Class attributes

- [Instructor] In this movie, we will learn how to define and use class attributes. Class attributes are properties that are related to a class generally, not to a specific instance. It's the same idea as we saw with class methods in the last movie. Class attributes will define shared values among all instances of the class. A class variable is recognizable because it has two @ signs at the start, where an instance variable only has one. This communicates that the variable has scope throughout every instance of the class. Any instance of the class can access it, it's like a shared variable. A class variable can be defined anywhere inside the class definition. It does not have to be inside a method. In this example, wheels is a class variable that's set to 2. There's also an instance variable for color that gets set whenever an object is initialized. Notice that the description instance method is able to access both the instance variable and the class variable. All instances that are…

Contents