Skip to content

Cheat sheet ​

Every line is a ready expression, its result on the right.

Text ​

mzs
"  OPERATOR ".lower.trim == "operator"  # → true
"Ivan Petrov".split.first  # → "Ivan"
"ivan@x.ru".split("@")[1]  # → "x.ru"
"one,two".split(",").map { it.upper }.join(" ")  # → "ONE TWO"
"привет".len  # → 6
"hello".reverse  # → "olleh"
"hello".capitalize  # → "Hello"
"ell" in "hello"  # → true
"/start".starts_with("/")  # → true
"Ivan".ljust(8, ".")  # → "Ivan...."
"a long text".truncate(8)  # → "a long …"
"a\nb\n".lines  # → ["a","b"]
"%.2f €" % [3.14159]  # → "3.14 €"
"Straße".casefold == "STRASSE".casefold  # → true

Interpolation is ${…}:

mzs
n = 3
"items: ${n}, total: ${n * 100}"  # → "items: 3, total: 300"

Collections ​

mzs
[1, 2, 3, 4].filter { it.even }  # → [2,4]
[1, 2, 3].map { it * 10 }  # → [10,20,30]
[1, 2, 3].sum  # → 6
[1, 2, 3].reduce(1) { (a, x) -> a * x }  # → 6
[3, 1, 2].sort  # → [1,2,3]
[3, 1, 2].sort { (a, b) -> b <=> a }  # → [3,2,1]
["bb", "a", "ccc"].sort_by { it.len }  # → ["a","bb","ccc"]
["bb", "a", "ccc"].max_by { it.len }  # → "ccc"
[1, 1, 2, 3, 3].uniq  # → [1,2,3]
"yes,no,yes".split(",").tally  # → {"yes":2,"no":1}
[1, 2, 3, 4, 5].each_slice(2)  # → [[1,2],[3,4],[5]]
[1, 2].zip(["a", "b"])  # → [[1,"a"],[2,"b"]]
[1, [2, [3]]].flatten  # → [1,2,3]
[1, nil, 2].compact  # → [1,2]
[].first ?? "empty"  # → "empty"
(1..5).array  # → [1,2,3,4,5]
3.times.map { it * it }  # → [0,1,4]
(1..1_000_000_000).seq.filter { it % 7 == 0 }.take(3).array  # → [7,14,21]

Dicts ​

mzs
{a: 1}.merge({b: 2})  # → {"a":1,"b":2}
{a: 1, b: 2}.keys  # → ["a","b"]
{a: 1}.get("z", 0)  # → 0
{a: {b: {c: 7}}}.dig("a", "b", "c")  # → 7
{a: 1, b: 2}.map { (k, v) -> "${k}=${v}" }.join("&")  # → "a=1&b=2"
{a: 1, b: 2}.filter { (_, v) -> v > 1 }  # → {"b":2}
{a: 1, b: 2, c: 3}.pick("a", "c")  # → {"a":1,"c":3}
[["a", 1], ["b", 2]].dict  # → {"a":1,"b":2}
[1, 2, 3, 4].group_by { it.even ? "even" : "odd" }  # → {"odd":[1,3],"even":[2,4]}

JSON ​

mzs
include json
json.parse('{"a": [1, 2]}').dig("a", 1)  # → 2
json.parse("[1, null, true]")  # → [1,null,true]
{a: 1, b: [2]}.json  # → "{\"a\":1,\"b\":[2]}"
try json.parse("{oops") else "broken JSON"  # → "broken JSON"
println(json.pretty({a: 1}))
text
{
  "a": 1
}

Numbers ​

mzs
7 / 2  # → 3
7.0 / 2  # → 3.5
7 % 3  # → 1
2 ** 10  # → 1024
1.256.round(2)  # → 1.26
2.5.round  # → 3
"42".int + 1  # → 43
"".int + 1200  # → 1200
"12abc".parse_int  # → nil
"3.5".float * 2  # → 7.0
255.str(16)  # → "ff"
[1, 2, 3, 4].mean  # → 2.5
5.clamp(1, 3)  # → 3
max(3, 7, 5)  # → 7

Regexes ​

mzs
"Hello!" ~ /hi|hello/i  # → true
"hello".index(/llo/)  # → 2
"+7 (999) 123-45-67".replace(/\D/, "")  # → "79991234567"
"a1 b22 c333".matches(/\d+/)  # → ["1","22","333"]
"2026-08-13".captures(/(\d+)-(\d+)-(\d+)/)[1]  # → "2026"
"foo=1;bar=2".matches(/(\w+)=(\d)/).dict  # → {"foo":"1","bar":"2"}
"John Smith".replace(/(\w+) (\w+)/, '\2 \1')  # → "Smith John"

Branching ​

mzs
x = 7
x > 3 ? "yes" : "no"  # → "yes"
nil ?? "default"  # → "default"
match x { in 1..5 -> "few"; else -> "many" }  # → "many"

Errors ​

mzs
try raise("forbidden") else "caught"  # → "caught"
try raise("forbidden") else (e) -> e["message"]  # → "forbidden"
try raise("no money", "billing") else (e) -> e["kind"]  # → "billing"
try 1 / 0 else (e) -> e["kind"]  # → "zero-division"
try {a: 1}.fetch("b") else (e) -> e["kind"]  # → "key"
nil?.upper  # → nil