ruby · level 1 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a box with a name on it.
A variable is like a box with a label. You put something in the box, write a name on the label, and from then on you say the name to get it back.
name = "Sam" puts the string "Sam" into a box called name. Whatever is inside the box is its . Read the = as put into, not equals.
To use the value, write the name with no quotes. puts name looks inside the box and shows Sam. With quotes, "name" is a string holding the word name itself.
name = "Sam"
puts name Misspell the name and Ruby gives a NameError, because no box has that label.
guess first
Box or word? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
snack = "popcorn"
puts snack
puts "snack"
your turn
Your turn. Invent a superhero.
Make up a superhero name and type it between the quotes on line 1. Then press run and watch line 3 look inside the box.
fix it
This crashes with a NameError. Can you fix it?
- Look at the word after
puts. Is it spelled the same as the box on line 1? - A letter is missing. The box is called colour.
- Change colur to colour, then press run.
nailed it
you made the computer remember. :D
- A variable is a box with a name on it.
=puts a value into the box.- Spell the name the same way every time.
new words in your collection
- variable
- value
This lesson isn’t saved. A free account remembers every lesson you finish.