Learn Ruby
Instance Variable
21.2
Lesson

Direction of car

Let’s add a method so that we could ask car1 which direction it is current going. The method name will be current_direction.

Here we run into the same issue. Method current_direction can’t access variable direction of method turn.

We want turn method to store the direction in an instance variable so that current_direction can access that instance variable.

If we add @ to a variable then that variable becomes instance variable. The special thing about an instance variable is that any instance method can access an instance variable.

In this case we are going to store the direction of the turn into an instance variable.