Tech News
← Back to articles

Lowtype: Elegant Types in Ruby

read original related products more articles

LowType

LowType introduces the concept of "type expressions" in method arguments. When an argument's default value resolves to a type instead of a value then it's treated as a type expression. Now you can have types in Ruby in the simplest syntax possible:

class MyClass include LowType def say_hello ( greeting : String ) # Raises exception at runtime if greeting is not a String. end end

Default values

Place | after the type definition to provide a default value when the argument is nil :

def say_hello ( greeting = String | 'Hello' ) puts greeting end

Or with keyword arguments:

def say_hello ( greeting : String | 'Hello' ) puts greeting end

Enumerables

Wrap your type in an Array[T] or Hash[T] enumerable type. An Array of String s looks like:

... continue reading