Rails に sorbet をインストールしてみる
Ruby Ruby on Rails
Published: 2019-08-06

やったこと

Rails に sorbet を導入します。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

$ rails --version
Rails 5.2.3

調査

Gemfile

gem 'sorbet', :group => :development
gem 'sorbet-runtime'
$ bundle install

sorbet の初期化

$ bundle exec srb init
# typed: true
class ApplicationController < ActionController::Base
  extend T::Sig

  private
  sig {params(x: Integer).returns(String)}
  def sorbet_init(x)
    # x.to_s
  end
end

型チェックの結果

$ bundle exec srb tc
app/controllers/application_controller.rb:20: Returning value that does not conform to method result type https://srb.help/7005
    20 |  def sorbet_init(x)
    21 |    # x.to_s
    22 |  end
  Expected String
    app/controllers/application_controller.rb:20: Method sorbet_init has return type String
    20 |  def sorbet_init(x)
          ^^^^^^^^^^^^^^^^^^
  Got NilClass originating from:
    app/controllers/application_controller.rb:20:
    20 |  def sorbet_init(x)
          ^^^^^^^^^^^^^^^^^^
Errors: 1

参考