TODO: This page is still a fragment. Contributions welcome!
Sorbet supports generics types. The syntax looks likes
MyClass[Elem]. For user
defined generic classes, it’s possible to make this valid Ruby syntax.
However, it’s not possible to change the syntax for classes in the Ruby standard library that should be generic. To make up for this, we use wrappers in the T:: namespace to represent them:
- This validates that the value is an array of any length where all elements of the array are the specified type.
- This validates that the value is a hash where all keys of the type specified
KeyType, and all values are of the type specified by
- This validates that the value is a hash where all keys of the type specified by
While you still can write Array or Hash inside a sig, they will be treated as
T::Hash[T.untyped, T.untyped] respectively.