An interactive Ruby lesson
Variables and assignment
A variable gives you a name for a value. Build on Values to calculate with names and change their values.
Choose descriptive lowercase names. Use underscores between words, as in book_count. This convention is called snake_case.
Read code from top to bottom. As in Values, p(...) displays a value on its own line.
Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.
The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.
1. Assign a value to a name
book_count = 3 assigns the value 3 to a local variable named book_count. Read the name later to use its value. Predict both lines of output.
Show expected output
3 3
Try it: Change 3 to 6. Predict the output, then run again.
2. Calculate with variables
Ruby uses the current values of the names in a calculation. Predict the total cost.
Show expected output
15
Try it: Change ticket_count to 4. Predict the total before running.
3. Assign a new value
Assigning again changes the value that the name refers to. Predict what each p displays.
Show expected output
4 9
Try it: Change the second assignment to score = 12. Predict which output line changes.
4. Update a number
Ruby evaluates the right side of = before assigning the result. So page_count = page_count + 2 uses the previous count. For a local variable, page_count += 3 means page_count = page_count + 3. Predict both counts.
Show expected output
12 15
Try it: Change += 3 to += 5. Predict both outputs before running.
5. Assign an integer to another name
saved_score = score assigns the current integer value of score to saved_score. Later reassignment of score does not reassign saved_score. Predict the two numbers.
Show expected output
9 4
Try it: Change the first assignment to score = 6. Predict both outputs, then run.
6. Compare with ==
= assigns a value. == compares values and produces true or false. A comparison here leaves score unchanged. Predict all four lines.
Show expected output
true false 7 10
Try it: Change the starting value to 10. Predict both comparisons and the numbers before running.
Practice on your own
Replace the code in the last editor with a program for these tasks. Predict the output, then run it.
- Assign
8tocoin_count. - Assign its current value to
starting_count. - Add
3tocoin_countwith+=. - Display
coin_count, thenstarting_count. - Display whether
coin_countequals11.
Show one solution and its output
coin_count = 8 starting_count = coin_count coin_count += 3 p(coin_count) p(starting_count) p(coin_count == 11)
Output:
11 8 true
Ruby documentation
See the Ruby 4.0 assignment documentation for local variables, reassignment, and abbreviated assignment such as +=.
Return to the Ruby skill tree to find the foundations that build on variables.