Skip to content
Strings and Interpolation
step 1/5

Reading — step 1 of 5

Learn

~1 min readStrings

Strings can use single or double quotes — but interpolation (#{...}) only works inside double-quoted strings.

name = "Alice"
age = 30
puts "Hi, #{name}. You are #{age} years old."
puts 'No interpolation here #{name}'   # literal #{name}

Strings are mutable in Ruby (unlike most languages). Methods that mutate the receiver end with ! by convention:

s = "hello"
s.upcase    # 'HELLO' — returns new string
s           # 'hello' — original unchanged
s.upcase!  # mutates s in place
s           # 'HELLO'

Useful methods: length, reverse, split, chars, include?, start_with?. The standard library is huge — when in doubt, type "foo".methods.sort.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…