Skip to content

Standard Library

The standard library is split across focused modules. The list will grow over time.


Low-level types and attributes required by the Amethyst runtime. Always linked — not user-facing in normal code.

Type Purpose
Symbol Lightweight interned string identifier (:name)
ControlFlowReturnException Non-local return from blocks
ControlFlowBreakException Non-local break from blocks
Attribute Purpose
@[AmethystName("...")] Preserves original snake_case name on compiled IL
@[AnonymousArgumentAttribute] Marks a positional parameter
@[KeywordArgumentAttribute] Marks a keyword parameter
@[Property] Forces a method to be emitted as a .NET property
Name Kind Purpose
Comparable[T] Protocol <=> ordering
Hashable Protocol hash() for use as hash keys
Equatable[T] Protocol == equality
DynamicMessages Mixin Opt-in arbitrary message dispatch

Extensions to the .NET class library. Included by default in all Amethyst projects.

Provides collection operations to any type that implements each. Included explicitly for Amethyst types, synthesised automatically for types implementing .NET’s IEnumerable[T]:

# Amethyst type — explicit inclusion
class NumberList
include Enumerable[Int]
def each(&block: (Int) -> Void): Void
@items.each(&block)
end
end
# .NET type — synthesised automatically
list = List[String].new # gets map, select, etc. via IEnumerable[T]

Available methods include: map, select, reject, reduce, each_with_index, flat_map, any?, all?, none?, count, first, last, min, max, min_by, max_by, sort, sort_by, group_by, zip, take, drop, each_slice, each_cons, to_array, to_hash.

Disposable is Amethyst’s name for .NET’s System.IDisposable. Amethyst types adopt it with implements Disposable and define dispose. .NET types that implement IDisposable are already Disposable.

use passes the object to a block and calls dispose when the block finishes, however it finishes: normally, by raising, or through a non-local return or break. It takes the place of C#’s using statement:

StreamReader.new("data.csv").use do |reader|
puts reader.read_to_end
end
  • What use returns (for example, the block’s value)
  • Asynchronous disposal (System.IAsyncDisposable)

Common string utilities on top of System.String.

Top-level methods available everywhere without imports, provided by Amethyst::Kernel which is mixed into every object:

puts "hello" # print with newline
print "hello" # print without newline
p value # inspect and print
pp value # pretty-print
rand(n) # random number 0...n
sleep(seconds) # pause execution

Amethyst::Kernel can be extended to add globally available methods:

extension class Amethyst::Kernel
def log(message: String): Void
puts "[#{Time.now}] #{message}"
end
end
# Available everywhere:
log "server started"

An RSpec-inspired testing framework, added as a dependency — not built into the SDK. This allows users to swap it for another framework if preferred.

Terminal window
am add Amethyst.Test
describe UserService do
let(:service) { UserService.new }
before do
# setup
end
context "when user is valid" do
it "saves the user" do
user = User.new(name: "Alice")
expect(service.save(user)).to be_truthy
end
end
context "when user is invalid" do
it "raises an error" do
expect { service.save(nil) }.to raise_error(ValidationException)
end
end
end
  • describe / context — test grouping
  • it — individual test case
  • let / let! — lazy and eager test values
  • before / after — setup and teardown hooks
  • expect(x).to matcher — assertions
  • Built-in matchers: eq, be_truthy, be_falsy, be_nil, raise_error, include, match, be_a, be_between