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
- Assigns a string and an integer to plain local variables.
#{}splices any expression into a double-quoted string.- Method calls like
upcaseandlengthwork inside the braces too.
Keywords and builtins used here
nameputs
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.
Step 1 of 3 in Variables & strings, step 1 of 29 in Language basics.