Classes
Definition
Section titled “Definition”class Userend
class Admin < User # single inheritanceendObject Construction
Section titled “Object Construction”.new is a class method inherited by all classes. It allocates a new instance,
calls initialize with the given arguments, and returns the instance:
user = User.new("Alice", 30)The return type of .new is always the class itself. Arguments to .new are
forwarded directly to initialize.
.new can be overridden to customise object creation — for example to implement
caching or factory logic:
class Connection def self.new(*args: Any): self existing = cache.find(*args) return existing if existing super.new(*args) endendinitialize
Section titled “initialize”initialize is the instance method called by .new after allocation. Its return
value is always ignored — the return type is Void. The @-prefixed parameter
shorthand for declaring and assigning fields is only available in initialize:
class User def initialize(@name: String, @age: Int) # @name and @age declared and assigned automatically endendinitialize is private by default and cannot be called directly on an instance.
Call .new on the class to construct objects.
Constructor and Field Inference
Section titled “Constructor and Field Inference”Fields are inferred from def initialize. Prefixing a parameter with @ automatically
declares the field and assigns it:
class User def initialize(@name: String, @age: Int) # @name: String and @age: Int are declared and assigned automatically endend
# Equivalent longhandclass User def initialize(name: String, age: Int) @name = name @age = age endendAll @ instance variables are private by default.
Properties
Section titled “Properties”Use property, getter, or setter to expose instance variables. Each automatically
creates a private backing field (@name, @email, etc.) and the corresponding
accessor methods:
class User property name: String # backing @name, getter + setter getter email: String # backing @email, getter only setter password: String # backing @password, setter onlyendDefault values can be provided:
class User property name: String = "Anonymous" getter role: Symbol = :userendCustom Properties
Section titled “Custom Properties”def name and def name= are automatically promoted to a .NET property pair by the
compiler when both are present:
class User def initialize(@first_name: String, @last_name: String) end
def full_name: String "#{@first_name} #{@last_name}" end
def full_name=(value: String): Void parts = value.split(" ") @first_name = parts[0] @last_name = parts[1] endendA lone def foo without a matching def foo= is ambiguous — use @[Property] to
force it to be emitted as a .NET property:
@[Property]def display_label: String "User: #{@name}"endAccess Modifiers
Section titled “Access Modifiers”| Modifier | Visibility |
|---|---|
public |
Accessible from anywhere (default) |
private |
Accessible within the class only |
protected |
Accessible within the class and subclasses |
internal |
Accessible within the same assembly |
protected follows C# semantics — accessible from the class and its subclasses only.
This differs from Ruby, where protected allows access by any instance of the same class.
Two forms are supported, and both apply equally to def, property, getter,
and setter declarations.
Inline (applies to the immediately following declaration only)
Section titled “Inline (applies to the immediately following declaration only)”private def secret: Voidend
protected property name: String
internal getter config: ConfigScoped (applies to all subsequent declarations until the next modifier)
Section titled “Scoped (applies to all subsequent declarations until the next modifier)”class Foo property name: String # public
private
property password: String # private def secret: Void # private end
public
def public_again: Void # public endendDefault access is public.
Split Getter/Setter Visibility
Section titled “Split Getter/Setter Visibility”property applies the same modifier to both its getter and setter. To give the
getter and setter different visibility, declare them separately — the compiler
merges them into a single backing field and .NET property:
class User getter name: String # public getter private setter name: String # private setterendThis compiles to a .NET property with public get and private set, sharing a
single @name backing field.
Class Methods
Section titled “Class Methods”Class methods are defined with def self.:
class User def self.create(name: String): User User.new(name) endend
User.create("Alice")Classes behave as objects at compile time (outside of runtime introspection). Class methods map to static methods in .NET IL.
Generic Classes
Section titled “Generic Classes”class Stack[T] def push(item: T): Void end
def pop: T endendType Constraints
Section titled “Type Constraints”Use < to express upper bounds (subtype/interface satisfaction). Multiple constraints
on a single parameter are joined with &.
class Stack[T < Comparable[T]]class Cache[K < Hashable & Equatable[K], V]For complex constraints or constraints that reference other type parameters, use a
where clause after the signature:
class Graph[T, E] where T < Node, E < Edge[T]class SortedList[T] where T < Comparable[T]Type constraints also apply to generic methods:
def sort[T < Comparable[T]](items: Array[T]): Array[T]def zip[T, U](a: Array[T], b: Array[U]): Array[Tuple[T, U]] where T < Equatable[U].NET Kind Constraints
Section titled “.NET Kind Constraints”Beyond subtype constraints, .NET-specific kind constraints use : since they are not
subtype relationships:
| Constraint | Meaning |
|---|---|
T: class |
T must be a reference type |
T: struct |
T must be a value type |
T: new |
T must have a parameterless constructor |
These are expressed in the where clause:
class Repository[T] where T < Entity, T: newclass ValueWrapper[T] where T: structAbstract Classes
Section titled “Abstract Classes”An abstract class cannot be instantiated directly — it exists to be subclassed.
Declare it with abstract class:
abstract class Shape abstract def area: Float abstract def perimeter: Float
def describe: String "Area: #{area}, Perimeter: #{perimeter}" endendAttempting to instantiate an abstract class is a compile error:
Shape.new # compile error — Shape is abstractAbstract Methods
Section titled “Abstract Methods”Abstract methods declare a required interface with no body. Both abstract def
and requires def are accepted — they are equivalent:
abstract class Animal abstract def speak: String # must be implemented by subclasses requires def breathe: Void # same — alternative keywordendA concrete subclass that does not implement all abstract methods is a compile error.
Concrete Subclasses
Section titled “Concrete Subclasses”class Circle < Shape def initialize(@radius: Float) end
def area: Float Math::PI * @radius ** 2 end
def perimeter: Float 2 * Math::PI * @radius endend
Circle.new(5.0).describe # "Area: 78.53..., Perimeter: 31.41..."super is a reference to the parent object. Call parent methods on it explicitly
by name — there is no implicit “call the same-named method” behaviour:
class Dog < Animal def initialize(name: String, @breed: String) super.initialize(name) # calls Animal#initialize end
def speak: String super.speak + " Woof!" # calls Animal#speak end
def introduce: String "My animal sound is: #{super.speak}" # calls Animal#speak from a different method endendIn a class method, super refers to the parent class, so super.new calls the
parent’s .new. super is not storable — it cannot be assigned to a variable or
passed as an argument.
super as a Type
Section titled “super as a Type”super in a type position refers to the parent class’s type:
class Dog < Animal def as_animal: super # return type is Animal self endendSee Types for the full self and super type rules.
Protocols, Mixins, and Extensions
Section titled “Protocols, Mixins, and Extensions”See Modules for full details on implements, include, and
extension class.