百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

low_type

> 编程语言
开源

Ruby 中的优雅类型

343 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Ruby 中的优雅类型

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:

ruby
class MyClass
  include LowType

  def say_hello(greeting: String)
    # Raises exception at runtime if greeting is not a String.
    # Or `config.type_checking = false` to annotate your code.
  end
end

✨ Features:

  • Inline types
  • Enable/disable per environment
  • Integrates with Sinatra and Raindeer frameworks
  • Exports to RBS [UNRELEASED]
  • Uninstall easily: lowtype uninstall, that's it! [UNRELEASED]

Default values

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

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

Or with keyword arguments:

ruby
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:

ruby
def say_hello(greetings: Array[String])
  greetings # => ['Hello', 'Howdy', 'Hey']
end

Represent a Hash with key => value syntax:

ruby
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:

ruby
def say_hello() -> { String }
  'Hello' # Raises exception if the returned value is not a String.
end

Return values can also be defined as nilable:

ruby
def say_hello(greetings: Array[String]) -> { String | nil }
  return nil if greetings.first == 'Goodbye'
  greetings.first
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:

ruby
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 replicate attr_[reader, writer, accessor] methods but also allow you to define and check types.

Type Reader

ruby
type_reader name: String # Creates a public method called `name` that gets the value of @name
name # Get the value with type checking

type_reader name: String | 'Cher' # Gets the value of @name with a default value if it's `nil`
name # Get the value with type checking and return 'Cher' if the value is `nil`

Type Writer

ruby
type_writer name: String # Creates a public method called `name=(arg)` that sets the value of @name
name = 'Tim' # Set the value with type checking

Type Accessor

ruby
type_accessor name: String # Creates public methods to get or set the value of @name
name # Get the value with type checking
name = 'Tim' # Set the value with type checking

type_accessor name: String | 'Cher' # Get/set the value of @name with a default value if it's `nil`
name # Get the value with type checking and return 'Cher' if the value is `nil`
name = 'Tim' # Set the value with type checking

ℹ️ Multiple Arguments

You can define multiple typed accessor methods just like you would with attr_[reader, writer, accessor]:

ruby
type_accessor name: String | nil, occupation: 'Doctor', age: Integer | 33
name # => nil
occupation # => Doctor (not type checked)
age = 'old' # => Raises ArgumentTypeError
age # => 33

ℹ️ To use the Array[]/Hash[] enumerable syntax with type accessors you must add using LowType::Syntax:

ruby
include LowType
using LowType::Syntax

Local variables

type()

alias: low_type()

To define typed local variables at runtime use the type() method:

ruby
my_var = type MyType | fetch_my_object(id: 123)

my_var is now type checked to be of type MyType when assigned to.

Don't forget that these are just Ruby expressions and you can do more conditional logic as long as the last expression evaluates to a value:

ruby
my_var = type String | (say_goodbye || 'Hello Again')

Syntax

[T] Enumerables

Array[T] and Hash[T] class methods represent enumerables in the context of type expressions. If you need to create a new Array/Hash then use Array.new()/Hash.new() or Array and Hash literals [] and {}. This is the same syntax that RBS uses and we need to get use to these class methods returning type expressions if we're ever going to have inline types in Ruby. RuboCop also suggests {} over Hash[] syntax for creating hashes.

ℹ️ To use the Array[]/Hash[] enumerable syntax with type() you must add using LowType::Syntax:

ruby
include LowType
using LowType::Syntax

| Union Types / Default Value

The pipe symbol (|) is used in the context of type expressions to define multiple types as well as provide the default value:

  • To allow multiple types separate them between pipes: my_var = TypeOne | TypeTwo
  • The last value/nil defined becomes the default value: my_var = TypeOne | TypeTwo | nil

ℹ️ If no default value is defined then the argument will be required.

Nilable values

  • Represent a nilable value with T | nil
  • Represent an empty hash with Hash | {}

-> { T } Return Type

The -> { T } syntax is a lambda without an assignment to a local variable. This is valid Ruby that can be placed immediately after a method definition and on the same line as the method definition, to visually look like the output of that method. It's inert and doesn't run when the method is called, similar to how default values are never called if the argument is managed by LowType. Pretty cool stuff yeah? Your type expressions won't keep re-evaluating in the wild , only on class load.

ℹ️ A method that takes no arguments must include empty parameters () for the -> { T } syntax to be valid; def method() -> { T }.

value(T) Value Expression

alias: low_value()

To treat a type as if it were a value, pass it through value() first:

ruby
def my_method(my_arg: String | MyType | value(MyType)) # => MyType is the default value

Performance

LowType evaluates type expressions on class load (just once) to be efficient and thread-safe. Then the defined types are checked per method call.

However, type() type expressions are evaluated as they are called at runtime on an instance, and this may impact performance.

Evaluation Validation ℹ️ Example
Method param types Class load Runtime def method(name: T)
Method return types Class load Runtime def method() -> { T }
Instance types Class load Runtime type_accessor(name: T)
Local types Runtime Runtime type(T)

Scope

LowType only affects the class that it's included into. Class methods Array[]/Hash[] are modified for the type expression enumerable syntax ([]) to work, but only for LowType's internals (using refinements) and not the included class. The type() method requires using LowType::Syntax ONLY if you want to use the enumerable syntax and will affect ONLY the [] class method of every Array[]/Hash[] in the class it's included into.

Config

Copy and paste the following and change the defaults to configure LowType:

…

Types

Basic types

  • String
  • Integer
  • Float
  • Array
  • Hash
  • nil represents an optional value

Complex types

  • Boolean - Accepts true/false) [UNRELEASED]
  • Enum - Usage: Enum[1, 2, 3] [CONCEPT STAGE]
  • Tuple (subclass of Array)
  • Status (subclass of Integer)
  • Headers (subclass of Hash)
  • HTML (subclass of String) - TODO: Check that string is HTML
  • JSON (subclass of String) - TODO: Check that string is JSON
  • XML (subclass of String) - TODO: Check that string is XML

Custom types

Any class/type that's available to Ruby is available to LowType.

ℹ️ LowType evaluates parameter types in both the binding of LowType and the binding of the class that did the include.

Integrations

Because LowType is low-level it should work with method definitions in any framework out of the box. With that in mind we go a little further here at free-software-by-shadowy-figure-co to give you that extra framework-specific-special-feeling:

Sinatra

include LowType in your modular Sinatra::Base subclass to get Sinatra specific return types.
LowType will automatically add the necessary content_type [UNRELEASED] and type check the return value:

ruby
require 'sinatra/base'
require 'lowtype'

class MyApp  { String }
    'body'
  end

  # Generic return type for Sinatra.
  get '/' do -> { Array[Integer, Hash, String] }
    [200, {}, '
Hello!
']
  end

  # Specific return type for Sinatra.
  get '/' do -> { Tuple[Status, Headers, HTML] }
    [200, {}, '
Hello!
']
  end
end

Providers

With Providers you can inject your dependencies automatically via the constructor:

ruby
class MyClass
  include LowType

  def initialize(my_dependency: Dependency)
    @my_dependency = my_dependency # => The dependency is injected.
  end
end

Raindeer

LowType is supported out of the box by the Raindeer framework. In fact, Raindeer is built on many other Low gems.

Rubocop

Because we're living in the future, Rubocop isn't ready for us. Put the following in your .rubocop.yml:

yaml
# Support LowType return value "-> { T }" syntax.
Style/TrailingBodyOnMethodDefinition:
  Enabled: false
Layout/IndentationConsistency:
  Enabled: false
Layout/MultilineBlockLayout:
  Enabled: false
Style/DefWithParentheses:
  Enabled: false
Lint/Void:
  Enabled: false

# Support Array[]/Hash[] syntax.
Style/RedundantArrayConstructor:
  Enabled: false

Installation

Add gem 'lowtype' to your Gemfile then:

bundle install

Advanced Techniques

Inheritance

You must include LowType in every class that you'd like to have type checking/annotations for.
However you can eliminate the need for this include in every child class of a parent via the inherited hook:

ruby
class Parent
  def self.inherited(child)
    child.include LowType
  end
end

class Child >Proxies: Parses AST
  Proxies->>Expressions: Stores
  LowType->>Expressions: Evaluates
  LowType->>Methods: Redefines
  Methods-->>Expressions: Validates per method call or disable per environment

Three distinct phases isolate concerns:

  1. File Load: Code is parsed into an Abstract Syntax Tree but not evaluated (see Lowkey)
  2. Class Load: Constants and expressions are evaluated into real Ruby objects and methods redefined
  3. Runtime: Method argument types and return types are optionally validated

Philosophy

Duck typing is beautiful. Ruby is an amazing language BECAUSE it's not typed. I don't believe Ruby should ever be fully typed, but you should be able to sprinkle in types into some areas of your codebase where you'd like self-documentation and a little reassurance that the right valu

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyrubysinatratypechecker

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言