What is a Variable?
A variable is a label that refers to a value stored in memory. Let’s explore what that means:
x = 5
The name of the variable (x in this example) is on the left side of =, and
its value (5 in this case) is on the right. = is the assignment
operator. Here it assigns the variable x to the value 5. When you first
assign a variable to a value, you also define that variable. A defined
variable has meaning in the program, e.g., x means 5.
By assigning a variable to 5, we indicate to the Ruby interpreter that the
integer 5 is worth storing in memory. The variable "stands in" for the value,
giving the programmer a convenient way to retrieve and manipulate data in more
complex problems. For the example below, remember we assigned the variable x
with a value of 5.
x + 2
=> 7
puts x
5
=> nil
Any object can be assigned to a variable (e.g., b = "dog"). Variables can also
be reassigned (a = b). Run each of the following lines from the file (by
clicking run_), and check out the result in the Repl.it shell.
Predict the new values of a and b. Type a in the shell and press enter. Do
the same for b.
a
=> 7.5
b
=> true
When b is reassigned to a, a refers to true. The value of b therefore
also becomes true. The later reassignment of a to 7.5 does not affect the
value of b.