×

ruby · level 1 · unit 3 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Put any value inside a string with #{}.

Joining with + gets fiddly. Instead, write #{} inside a string and put a value between the curly brackets. Ruby swaps in the value for you. This is called , and it only works with double quotes.

Sometimes you need to a value, changing it from one kind to another. .to_i turns a string into an integer, so "7".to_i is 7. .to_s turns a number into a string.

gets always hands back a string, even when someone types a number. To do maths with the answer, convert it: age = gets.chomp.to_i.

name = "Mia"
age = 9
puts "#{name} is #{age} years old."
puts "Next year, #{name} will be #{age + 1}."
puts "7".to_i + 3
puts "Score: " + 10.to_s

Ruby works out anything inside #{}, even sums like #{age + 1}.