typestar

String interpolation in Ruby

Variables, strings, and the interpolation Ruby reaches for constantly.

name = "typestar"
year = 1995
puts "#{name} practices Ruby, born #{year}"

greeting = "hello"
shout = greeting.upcase
puts "#{greeting} became #{shout}"

# interpolation runs any expression, not just variable lookups
puts "next year is #{year + 1}"
puts "#{name.length} characters"

How it works

  1. Assigns a string and an integer to plain local variables.
  2. #{} splices any expression into a double-quoted string.
  3. Method calls like upcase and length work inside the braces too.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
281
Tokens
57
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 36 seconds.

Type this snippet

Step 1 of 3 in Variables & strings, step 1 of 29 in Language basics.

Next →

String interpolation in other languages