Tutorial
Browser runtime: loading…
Sapphire is a small, Ruby-inspired language with objects, blocks, and optional type annotations. This page walks through the basics; each runnable snippet uses the same WASM engine as the playground.
Hello, Sapphire
print writes a line. Double-quoted strings can interpolate with
# — same idea as Ruby.
Values and variables
Assign with =. Types are often inferred; you can mix numbers and strings in messages.
Methods
Define functions with def. The last expression is the return value unless you use
return. Annotations like -> Int document intent and help the type checker.
Classes
class introduces a type. attr declares state;
.new constructs an instance. Inside methods, self is the receiver.
Inheritance
Subclasses use < Parent. Call overridden hooks with super.
Run the example, then try changing dimensions or color and run again.
Blocks and iterators
Arrays support familiar iterators: map, each,
reduce, and friends. Blocks use |x| parameters.
Optional types
You can annotate parameters and return types where it helps clarity. Omit them where inference is enough.
More patterns: recursion, guards, and multi-branch returns.
Errors
Raise with raise. Handle failures with begin /
rescue. You can also attach rescue to a method body for small scopes.
Parsing with validation, begin/rescue, and inline rescue on a method.
Next steps
- Open the playground for a blank editor and the full keyboard shortcuts.
- Follow Install on the home page to run Sapphire locally.
- Browse the GitHub repository for the compiler, stdlib, and issues.