Skip to content

Classes

class User
end
class Admin < User # single inheritance
end

.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)
end
end

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
end
end

initialize is private by default and cannot be called directly on an instance. Call .new on the class to construct objects.

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
end
end
# Equivalent longhand
class User
def initialize(name: String, age: Int)
@name = name
@age = age
end
end

All @ instance variables are private by default.

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 only
end

Default values can be provided:

class User
property name: String = "Anonymous"
getter role: Symbol = :user
end

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]
end
end

A 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}"
end
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: Void
end
protected property name: String
internal getter config: Config

Scoped (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
end
end

Default access is public.

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 setter
end

This compiles to a .NET property with public get and private set, sharing a single @name backing field.

Class methods are defined with def self.:

class User
def self.create(name: String): User
User.new(name)
end
end
User.create("Alice")

Classes behave as objects at compile time (outside of runtime introspection). Class methods map to static methods in .NET IL.

class Stack[T]
def push(item: T): Void
end
def pop: T
end
end

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]

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: new
class ValueWrapper[T] where T: struct

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}"
end
end

Attempting to instantiate an abstract class is a compile error:

Shape.new # compile error — Shape is abstract

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 keyword
end

A concrete subclass that does not implement all abstract methods is a compile error.

class Circle < Shape
def initialize(@radius: Float)
end
def area: Float
Math::PI * @radius ** 2
end
def perimeter: Float
2 * Math::PI * @radius
end
end
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
end
end

In 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 in a type position refers to the parent class’s type:

class Dog < Animal
def as_animal: super # return type is Animal
self
end
end

See Types for the full self and super type rules.


See Modules for full details on implements, include, and extension class.