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 Strings looks like: def say_hello(greetings: Array[String]) greetings # => ['Hello', 'Howdy', 'Hey'] end Represent a Hash with key => value syntax: def say_hello(greetings: Hash[String => Integer]) greetings # => {'Hello' => 123, 'Howdy' => 456, 'Hey' => 789}) end Return values After your method's parameters add -> { T } to define a return value: def say_hello() -> { String } 'Hello' # Raises exception if the returned value is not a String. end Return values can also be defined as nilable: def say_hello(greetings: Array[String]) -> { String | nil } return nil if greetings.first == 'Goodbye' end If you need a multi-line return type/value then I'll even let you put the -> { T } on multiple lines, okay? I won't judge. You are a unique flower 馃尭 with your own style, your own needs. You have purpose in this world and though you may never find it, your loved ones will cherish knowing you and wish you were never gone: def say_farewell_with_a_long_method_name(farewell: String) -> { ::Long::Name::Space::CustomClassOne | ::Long::Name::Space::CustomClassTwo | ::Long::Name::Space::CustomClassThree } # Code that returns an instance of one of the above types. end Instance variables To define typed @instance variables use the type_[reader, writer, accessor] methods. These repli...
First seen: 2025-12-02 17:54
Last seen: 2025-12-02 21:55