ESC
Type to search...
S
Soli Docs

Core Functions

I/O operations, type conversion, array/hash manipulation, string functions, and math utilities.

I/O Functions

print(value)

Prints a value to standard output without a newline.

print("Hello")
print(" World")  # Output: Hello World
println(value)

Prints a value to standard output with a newline.

println("Hello World")
println(42)
puts(value)

Alias for println - prints a value to standard output with a newline. Ruby-style alternative.

puts("Hello World")
puts 42
input(prompt?)

Reads a line of input from the user. Returns the user's input as a String.

name = input("Enter your name: ")
puts "Hello, " + name

Type Functions

type(value)

Returns the type name of a value as a string.

type(42)        # "int"
type("hello")   # "string"
type([1, 2, 3]) # "array"
type(null)      # "null"
value.is_a?(class_name)

Returns whether a value is an instance of the specified class. For instances (including models), checks class hierarchy. For primitives, supports "int", "numeric", "object" type names.

user = User.find("123")
user.is_a?("User")      # true
user.is_a?("Model")     # true (inheritance)
user.is_a?("String")    # false
123.is_a?("Int")         # true
123.is_a?("numeric")    # true
"hello".is_a?("String")  # true
[1, 2].is_a?("Array")    # true
defined(name)

Checks if a variable is defined in the current scope chain. Returns true if the variable exists, false otherwise.

x = 42;
defined("x");        # true
defined("y");        # false
fn check(val) {
  if defined("val") { "exists" } else { "not set" }
}
const_get(name)

Resolves a string name to its value in the current scope chain. Returns the value (class, function, variable, etc.) if defined, or null otherwise.

class DemoUser { }

let cls = const_get("DemoUser");
cls.new();                   # Creates a DemoUser instance

let not_found = const_get("NonExistent");
assert_null(not_found);
str(value)

Convert a value to a string.

str(42)       # "42"
str(3.14)     # "3.14"
str(true)     # "true"
int(value)

Convert a value to an integer.

int("42")     # 42
int(3.7)      # 3
float(value)

Convert a value to a float.

float("3.14") # 3.14
float(42)     # 42.0
len(value)

Returns the length of a string, array, or hash. Also available as a method: .len, .length, .size are all aliases.

len("hello")      # 5
len([1, 2, 3])    # 3
len({"a": 1})     # 1

# Method syntax (all equivalent)
[1, 2, 3].len     # 3
[1, 2, 3].length  # 3
[1, 2, 3].size    # 3

Array Functions

Tip: Most array functions are also available as Array class methods. Use arr.push(42) instead of push(arr, 42) for a more object-oriented style.

push(array, value) / array.push(value)

Add an element to the end of an array.

arr = [1, 2]
push(arr, 3)     # arr is now [1, 2, 3]

# Or use class method style:
arr2 = [1, 2]
arr2.push(3)     # arr2 is now [1, 2, 3]
pop(array) / array.pop

Remove and return the last element from an array.

arr = [1, 2, 3]
last = pop(arr)  # last is 3, arr is [1, 2]

# Or use class method style:
arr2 = [1, 2, 3]
last2 = arr2.pop  # last2 is 3, arr2 is [1, 2]
range(start, end, step?)

Creates an array of numbers from start to end (exclusive).

range(0, 5)      # [0, 1, 2, 3, 4]
range(1, 10, 2)  # [1, 3, 5, 7, 9]
range(5, 0, -1)  # [5, 4, 3, 2, 1]
clear(array | hash) / array.clear / hash.clear

Remove all elements from an array or all entries from a hash.

arr = [1, 2, 3]
clear(arr)  # arr is now []

h = {"a": 1, "b": 2}
h.clear   # h is now {}

Hash Functions

Tip: Most hash functions are also available as Hash class methods. Use h.keys instead of keys(h) for a more object-oriented style.

keys(hash) / hash.keys

Returns an array of all keys in a hash.

h = {"name": "Alice", "age": 30}
keys(h)       # ["name", "age"]
h.keys      # ["name", "age"]
values(hash) / hash.values

Returns an array of all values in a hash.

h = {"name": "Alice", "age": 30}
values(h)     # ["Alice", 30]
h.values    # ["Alice", 30]
entries(hash) / hash.entries

Returns an array of [key, value] pairs.

h = {"name": "Alice", "age": 30}
entries(h)    # [["name", "Alice"], ["age", 30]]
h.entries   # [["name", "Alice"], ["age", 30]]
has_key(hash, key) / hash.has_key(key)

Check if a key exists in a hash.

has_key({"a": 1}, "a")    # true
has_key({"a": 1}, "b")    # false
h = {"a": 1}; h.has_key("a")  # true
hash.merge(other)

Merge two hashes together. Values from other take precedence.

{"x": 1}.merge({"y": 2})     # {"x": 1, "y": 2}
h1 = {"a": 1}; h2 = {"a": 2}; h1.merge(h2)  # {"a": 2}
from_entries(array)

Create a hash from an array of [key, value] pairs.

from_entries([["a", 1], ["b", 2]])  # {"a": 1, "b": 2}

String Functions

Note: String functions are now primarily available as String class methods. Use "hello".split(",") instead of split("hello", ",").

str.split([sep])

Split a string by a separator. Returns an array of string parts. sep is optional and defaults to " " (a single space).

"a,b,c".split(",")        # ["a", "b", "c"]
"hello world".split(" ")  # ["hello", "world"]
"hello world".split       # ["hello", "world"]  (sep defaults to " ")
"hello world".split()     # ["hello", "world"]
str.join(sep)

Join string parts with a separator.

"a,b,c".join("-")    # "a-b-c"
str.contains(sub)

Check if a string contains a substring.

"hello world".contains("world")  # true
"hello".contains("xyz")          # false
str.starts_with(prefix) / str.ends_with(suffix)

Check if a string starts with a prefix or ends with a suffix.

"hello".starts_with("hell")  # true
"hello".ends_with("llo")     # true
str.index_of(substr)

Returns the index of the first occurrence of substring, or -1 if not found.

"hello world".index_of("world")  # 6
"hello".index_of("xyz")          # -1
str.substring(start, end)

Extract a substring from start to end (exclusive).

"hello world".substring(0, 5)   # "hello"
"hello world".substring(6, 11)  # "world"
str.upcase / str.downcase / str.trim()

Transform string case and remove whitespace.

"hello".upcase      # "HELLO"
"HELLO".downcase    # "hello"
"  hi  ".trim       # "hi"
str.replace(from, to)

Replace all occurrences of a substring.

"hello world".replace("world", "soli")  # "hello soli"
str.lpad(width, pad_char?) / str.rpad(width, pad_char?)

Pad a string to reach the specified width.

"hi".lpad(5)           # "   hi"
"hi".rpad(5)           # "hi   "
"hi".lpad(5, "0")      # "000hi"
String.new(value)

Create a String instance from various value types.

String.new(42)       # "42"
String.new(3.14)     # "3.14"
String.new(true)     # "true"

Math Functions

abs(n) / min(a, b) / max(a, b)

Common math operations.

abs(-5)       # 5
min(3, 7)     # 3
max(3, 7)     # 7
sqrt(n) / pow(base, exp)

Square root and power functions.

sqrt(16)      # 4.0
pow(2, 3)     # 8.0
clock()

Returns the current Unix timestamp as a float with sub-second precision.

start = clock
# ... do work ...
elapsed = clock() - start

File I/O Functions

slurp(path)

Read an entire file into a string.

content = slurp("config.json")
barf(path, content)

Write content to a file.

barf("output.txt", "Hello, World!")

File Class

Static methods for file operations.

File.read(path)

Read file contents as string.

content = File.read("config.json")
File.write(path, content)

Write content to file.

File.write("output.txt", "Hello!")
File.exists(path)

Check if file exists.

if File.exists("config.json")
File.delete(path)

Delete a file.

File.delete("old.txt")
File.glob(pattern)

Match files using glob pattern (non-recursive).

files = File.glob("docs/*.md")
File.glob_recursive(pattern)

Match files using glob pattern (recursive).

files = File.glob_recursive("docs/**/*.md")
File.lines(path)

Read file as array of lines.

lines = File.lines("data.txt")
File.is_dir(path)

Check if path is a directory.

File.is_file(path)

Check if path is a file.

File.size(path)

Get file size in bytes.

File.modified(path)

Get file modification timestamp (Unix epoch in seconds).

time = File.modified("config.json")
File.copy(src, dest)

Copy file from source to destination.

File.rename(old, new)

Rename/move a file.

File.append(path, content)

Append content to file.