Double-quoted strings support escape sequences and interpolation with #{}:
name = "World"
puts "Hello, #{name}!"Expressions work inside interpolation:
x = 10
puts "#{x} squared is #{x * x}"Note: Nested double quotes inside interpolation are not supported. Use a variable instead:
x = h["key"]; puts "#{x}"
Single-quoted strings are raw — no escape processing and no interpolation:
puts 'hello\nworld' # prints: hello\nworld (literal, no newline)
puts '\x1b[32mgreen' # prints: \x1b[32mgreen (literal, no ANSI)
puts 'no #{interpolation}' # prints: no #{interpolation}Only \\ (literal backslash) and \' (literal single quote) are recognized:
puts 'it\'s raw' # prints: it's raw
puts 'back\\slash' # prints: back\slashRaw strings are useful for regex patterns, Windows paths, and test assertions where you need exact literal text.
Heredocs are multiline string literals. Use <<DELIM to start a heredoc and DELIM on its own line to close it. Delimiters must be uppercase ([A-Z_][A-Z0-9_]*).
Works like double-quoted strings — #{...} is evaluated:
name = "World"
html = <<HTML
<h1>Hello #{name}</h1>
<p>Welcome!</p>
HTML
puts htmlStrips common leading whitespace so you can indent the body with your code:
name = "World"
page = <<~HTML
<h1>Hello #{name}</h1>
<p>Welcome!</p>
HTML
puts page # no leading spaces in outputNo interpolation — content is literal, like single-quoted strings:
template = <<'CODE'
def #{method_name}
puts "hello"
end
CODE
puts template # #{method_name} printed literallyCombines indent stripping with literal content:
config = <<~'YAML'
name: myapp
version: 1.0
YAMLThe closing delimiter can be indented — leading whitespace is ignored when matching.
Extract a substring with text[start, length] — same syntax as array slicing:
text = "hello world"
puts text[0, 5] # hello
puts text[6, 5] # worldOut-of-bounds slices are clamped silently — no panics:
text = "hello"
puts text[3, 100] # lo — clamped to end
puts text[99, 5] # "" — empty stringConcatenation with +:
greeting = "Hello" + ", " + "World!"
puts greetingRaw and double-quoted strings can be concatenated:
puts 'raw\n' + "escaped\n" # raw\nescaped<newline>Strings support all comparison operators with lexicographic ordering:
if "apple" < "banana"
puts "apple comes first"
end
if "hello" == "hello"
puts "equal"
endImport str for string utilities:
use "str"
puts str.upper("hello") # HELLO
puts str.lower("HELLO") # hello
puts str.trim(" hello ") # hello
puts str.contains("hello", "ell") # true
puts str.starts_with("hello", "he") # true
puts str.ends_with("hello", "lo") # true
puts str.replace("hello", "l", "r") # herro
puts str.index("hello", "ll") # 2
parts = str.split("a,b,c", ",")
puts parts
puts str.join(parts, " | ") # a | b | cNext: Arrays