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.

hello.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Values and variables

Assign with =. Types are often inferred; you can mix numbers and strings in messages.

values.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

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.

methods.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Classes

class introduces a type. attr declares state; .new constructs an instance. Inside methods, self is the receiver.

counter.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Inheritance

Subclasses use < Parent. Call overridden hooks with super. Run the example, then try changing dimensions or color and run again.

shapes.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Blocks and iterators

Arrays support familiar iterators: map, each, reduce, and friends. Blocks use |x| parameters.

blocks.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Optional types

You can annotate parameters and return types where it helps clarity. Omit them where inference is enough.

types.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

More patterns: recursion, guards, and multi-branch returns.

math.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Errors

Raise with raise. Handle failures with begin / rescue. You can also attach rescue to a method body for small scopes.

errors.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Parsing with validation, begin/rescue, and inline rescue on a method.

errors-full.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

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.