...

/

Reusing Variable Names

Reusing Variable Names

This lesson explains how variable names can be reused in Ruby.

It is also important to keep in mind that a name is unique, in the sense that the same name can only be assigned to one value (object) at a time.

In other words, if you assign different values to the same variable, then assignments that happen later will simply overwrite previous ones. Like so:

Press + to interact
number = 4
number = number * 3
puts number + 2

The first line assigns the name number to the number 4. The second line re-assigns it to another object.

Getting back to our post-its metaphor… this would stick a post-it with the name number on one thing, and then later ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy