Programs and the world / Built-in methods
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
03 Programs and the world / 05
Built-in methods
Every method there is on built-in values. If it isn't here, it doesn't exist.
Numbers, Strings, arrays and hashes come with a small set of methods, and this page lists all of them, grouped by the type they belong to. Find the type, then the method: each table says what a call gives. A few rules hold across every table:
- Marked changes
- The method changes the value it's called on, so it can only be called on a
var. - Is it empty?
- There is no method for that. Write
items.length == 0, which says exactly what is checked. - Methods with blocks
each,map,filter,find,anyandallare on their own page: Blocks.- Enum values
- Enum values have no methods.
Int and Float
An Int has one method:
| Method | Gives |
|---|---|
n.abs() | the value without its sign |
n.quotient(d) | how many whole times d goes into n, rounded down (see arithmetic) |
A Float can also be rounded, to a whole number or to a number of decimals (see Rounding):
| Method | Gives |
|---|---|
x.abs() | the value without its sign |
x.floor(), x.ceil(), x.round() | an Int |
x.floor(d), x.ceil(d), x.round(d) | a Float with d decimals |
String
None of a String's methods change it: each gives a new value. Lengths and positions are counted in characters, and positions start at 0.
Length, case and spaces
| Method | Gives |
|---|---|
text.length | the number of characters |
text.upcase(), text.downcase() | the text in upper or lower case |
text.trim() | the text without spaces, tabs or line breaks at either end |
Searching
| Method | Gives |
|---|---|
text.contains("x") | true if "x" appears in the text |
text.contains("x", at: Placement.start) | true if the text starts with "x"; Placement.finish, if it ends with it |
text.index_of("x") | where "x" first starts, counted in characters, or nil |
Cutting and changing
| Method | Gives |
|---|---|
text.split(",") | an array of the pieces between each ",", keeping empty pieces |
text.slice(from: 3) | the characters from the 4th on (they're counted from 0) |
text.slice(from: 0, to: 2) | the characters from the 1st up to, not with, the 3rd |
text.replace("x", by: "y") | the text with every "x" made "y" |
text.characters | an array of the characters, each a String |
slice counts characters, as length does, so "blåbær".slice(from: 2, to: 4) is "åb". Either end may be from 0 to the length. Outside that, or with to before from, it's an error that says which.
replace with "" to look for is an error, since "" is between every two characters.
const line = "## Install"
print(line.slice(from: 3)) # prints Install
print(line.slice(from: 0, to: 2)) # prints ##
print(line.replace("#", by: "-")) # prints -- Install
print(line.index_of("I")) # prints 3
print("blåbær".characters) # prints ["b", "l", "å", "b", "æ", "r"]
print("abc".slice(from: 4)) # error: can't slice from 4: the text has 3 charactersFor trimming, splitting and searching there are trim, split and contains, and join, a method of arrays, puts the pieces back together into one String:
print(" Hi ".trim() + "|") # prints Hi|
print("a,b,,c".split(",")) # prints ["a", "b", "", "c"]
print("hello".contains("ell")) # prints true
print("hello".contains("he", at: Placement.start)) # prints true
print("hello".contains("lo", at: Placement.finish)) # prints true
print(["Ada", "Grace"].join(", ")) # prints Ada, Grace
print([1, 2].join(", ")) # error: join needs an array of Strings, got a [Int]; convert the items first with map and String(...)Bytes
| Method | Gives |
|---|---|
bytes.length | the number of bytes |
bytes[i] | the byte at i, counted from 0, as an Int |
bytes.contains(other) | true if other appears in them |
bytes.contains(other, at: Placement.start) | true if they start with other; Placement.finish, if they end with it |
bytes.slice(from: 3) | the bytes from the 4th on |
bytes.slice(from: 0, to: 2) | the bytes from the 1st up to, not with, the 3rd |
bytes.text() | the text they hold, a String! (kind not_text if it isn't UTF-8) |
Array
| Method | Gives |
|---|---|
items.length | the number of elements |
items.contains(x) | true if an element equals x |
items.join(", ") | one String; the array must be a [String] |
items.push(x) | changes adds x at the end |
items.first() | the first element, or nil |
items.unique() | a new array with each value once: the first of each, in order |
items.limit(n), items.skip(n) | a new array of the first n elements; of all but the first n |
items.each, map, filter, find, any, all | see Blocks |
items.sort, sum, min, max, average, tally | see Blocks: sorting and figures |
items.group | see Blocks: groups |
Hash
| Method | Gives |
|---|---|
ages.length | the number of keys |
ages.get(key) | the value, or nil |
ages.get(key, fallback: other) | the value, or other |
ages.has(key) | true if the key exists |
ages.keys, ages.values | arrays, in order |
ages.set(key, to: value) | changes adds or replaces |
ages.remove(key) | changes removes the key if present |
ages.each | see Blocks |
Time
| Method | Gives |
|---|---|
time.plus(days: d, ...), time.minus(...) | a later, or earlier, Time (see Time) |
later.since(earlier) | a Duration |
time.in(zone) | a LocalTime (see Zones) |
Zone
| Method | Gives |
|---|---|
zone.name | a String: Europe/Copenhagen |
zone.parse(text) | a Time!, the moment the zone's clocks show that time |
LocalTime
| Method | Gives |
|---|---|
local.year | an Int; and so month, day, hour, minute, second and millisecond |
local.weekday | a Weekday |
local.date | a String, the day: 2026-11-24 |
local.offset | a Duration, how far from UTC |
local.time, local.zone | its Time and its Zone |
Duration
| Method | Gives |
|---|---|
duration.days | how long, in whole days, rounded toward zero; and so hours, minutes, seconds and milliseconds |
Random
| Method | Gives |
|---|---|
random.int(below: n) | changes a number from 0 up to, not with, n |
random.float() | changes a Float from 0.0 up to, not with, 1.0 |
random.pick(items) | changes one of the array's items |