Exercise 2 of 10 · Variables
Name Tag
What you will make
A conference name tag that prints a person's name and role from two variables, instead of typed-in text.
The one new idea: A variable stores a value, and #{...} drops it into a string
A program that only ever prints fixed text is not much of a program. Variables are how a value gets a name you can reuse, and interpolation is how that value finds its way back into a sentence — the two ideas underneath almost everything you will write next.
Go straight to the code ↓Variables, and putting them inside text
A variable is a name you give to a value so you can use it again without retyping it. name = "Priya" does not print anything by itself — it just stores the text "Priya" under the name name, ready for later.
To get a variable's value into a sentence, Ruby uses interpolation: write #{ then the variable name then }, anywhere inside a double-quoted string. Ruby swaps that whole chunk for whatever the variable currently holds, and leaves everything else in the string untouched.
city = "Pune"
puts "Weather report for #{city}"That prints Weather report for Pune. The #{...} is what brings the variable to life inside the string; without it, the braces would just be ordinary characters.
A worked example
A luggage tag rather than a name tag, so the fix in the editor stays yours to make:
owner = "Devi"
flight = "AI 202"
puts "TAG: #{owner}"
puts "FLIGHT: #{flight}"TAG: Devi
FLIGHT: AI 202Two variables, two interpolated lines, and the values land exactly where the #{...} sat in the string.
Your turn
The editor holds two variables, name and role, and a small name tag that should print both. Press Run first: the top line already shows Priya correctly, because it uses #{name}. The line below it still shows the word ROLE, typed in plain text, instead of the value stored in the role variable.
Rewrite that one line so it interpolates role the same way the line above interpolates name. Change nothing else.
If something goes wrong
If the line prints #{role} word for word instead of the value, the # in front of the braces went missing, or the string is wrapped in single quotes rather than double ones — Ruby only interpolates inside double quotes.
If Ruby stops the program entirely with NameError: undefined local variable or method, check the spelling inside the braces against the spelling on the line where the variable was created. Ruby matches names exactly, including capitalisation.
Change the role to something else, like "Ruby Explorer", and run it again — the printed tag should follow your edit without touching any other line.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing 'role' with single quotes instead of double
- Ruby only interpolates inside double-quoted strings. '#{role}' with single quotes prints the literal text #{role}, braces and all, because single quotes turn off every special meaning except the quote itself.
- Leaving off the # before the braces
- {role} on its own is not interpolation, just characters sitting in the string, so the line prints { role } instead of the value the role variable holds.
- Misspelling the variable name inside the braces
- #{Role} or #{rol} refers to a name Ruby has never seen, and the program stops with NameError: undefined local variable or method — variable names in Ruby are case-sensitive and typed exactly.
Want a blank editor instead? Open the Ruby playground.