Strings
Quotes
"…" understands escapes and interpolation. '…' is a raw string: only \' and \\ are escaped.
"a\tb".len # → 3
'a\tb'.len # → 4
'it\'s' # → "it's"Both forms may span several lines — the line break in the source goes into the value.
Interpolation
| Form | What it inserts |
|---|---|
${expression} | any expression, a local variable included |
$name | the host variable $name |
$ with no name and no { | the $ sign itself |
name = "Ann"
items = [3, 4]
"Hello, ${name}!" # → "Hello, Ann!"
"sum: ${items.sum * 10} ₽" # → "sum: 70 ₽"
"price: 100 $" # → "price: 100 $"
"${nil}|${true}|${[1, 2]}|${{a: 1}}" # → "|true|[1,2]|{\"a\":1}"A local variable goes only in ${…}. "$name" reads the host variable, not the local one:
name = "Ann"
"$name" # → ""
"${name}" # → "Ann"'…' inserts nothing: '${x}' stays as is.
Escapes
| Escape | Value |
|---|---|
\n \t \r \0 | line feed, tab, carriage return, null character |
\\ \" \' | backslash, quotes |
\$ | the $ sign |
\u{X…} | a character by code point, 1–6 hex digits: \u{1F600} |
\x41 | a byte |
"\u{1F600}" # → "😀"
"\x41" # → "A"
"10\$ off" # → "10$ off"Heredoc
<<~TAG takes the lines below up to the line TAG and strips the common indent. <<~'TAG' — no interpolation, no escapes.
name = "Ann"
text = <<~MSG
Hello, ${name}!
Your order is ready.
MSG
text # → "Hello, Ann!\n Your order is ready.\n"raw = <<~'TPL'
${not substituted} and \n are two characters
TPL
raw # → "${not substituted} and \\n are two characters\n"The body always ends with a line break.
Characters, not bytes
Length, indices and slices count Unicode characters.
s = "привет"
s.len # → 6
s.bytes.len # → 12
s[0] # → "п"
s[-1] # → "т"
s[1, 3] # → "рив"
s[10] # → nil
"abc".chars # → ["a","b","c"]
"я".upper # → "Я"s[i, n] is the substring of length n from position i. An index out of bounds gives nil, not an error.
Strings are immutable, every method returns a new string:
s = "cat"
s[0] = "C"type: cannot assign to an index of stringCommon methods
| Method | Example |
|---|---|
upper lower | "Ok".lower → "ok" |
trim | " x ".trim → "x" |
split | "a,b".split(",") → ["a","b"] |
join | ["a", "b"].join("-") → "a-b" |
replace | "a-b".replace("-", "+") → "a+b" |
has | "hello".has("ell") → true |
int float | "42".int → 42 |
" Hello, World ".trim.lower # → "hello, world"
"a-b-c".split("-") # → ["a","b","c"]
"12abc".int # → 12
"abc".int # → 0.int and .float never fail: what does not parse becomes 0. The full list is in Strings in the library.
Formatting: %
template % arguments is the same as format(template, …). A single argument need not be wrapped in an array.
| Specifier | What it prints |
|---|---|
%s | the value as a string |
%d | an integer |
%f %.2f | a float |
%x %o %b | an integer in base 16, 8, 2 |
%j | JSON |
%% | the % sign |
"%s: %d pcs" % ["apples", 3] # → "apples: 3 pcs"
"%.2f" % 3.14159 # → "3.14"
"%05.1f" % 3.14159 # → "003.1"
"%-6s|" % "ab" # → "ab |"
"%x" % 255 # → "ff"
"%d%%" % 50 # → "50%"
"%<name>s, %<age>d" % {name: "Ann", age: 30} # → "Ann, 30"
format("%s-%s", "a", "b") # → "a-b"