Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
rails newと同時に型を書く
Search
a_fujisaki
January 09, 2025
Programming
6
750
rails newと同時に型を書く
a_fujisaki
January 09, 2025
Tweet
Share
More Decks by a_fujisaki
See All by a_fujisaki
“Rails × 型”での2年間を振り返る
aki19035vc
2
1k
rbs-inlineを使ってみた
aki19035vc
0
580
Emacs × LSP × Steep
aki19035vc
0
140
MiniMagickの型を生成したい
aki19035vc
0
81
最近追加した型の紹介とその振り返り
aki19035vc
0
490
Other Decks in Programming
See All in Programming
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
210
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
2.7k
2025.01.17_Sansan × DMM.swift
riofujimon
2
680
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
310
ESLintプラグインを使用してCDKのセオリーを適用する
yamanashi_ren01
2
380
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
360
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
240
時計仕掛けのCompose
mkeeda
1
210
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
2.6k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
450
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
510
AWSマネコンに複数のアカウントで入れるようになりました
yuhta28
2
150
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
19k
The Invisible Side of Design
smashingmag
299
50k
Become a Pro
speakerdeck
PRO
26
5.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
20
2.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Documentation Writing (for coders)
carmenintech
67
4.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Typedesign – Prime Four
hannesfritz
40
2.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Transcript
rails newと同時に型を書く Roppongi.rb#26 <2025-01-09> @aki19035vc
自己紹介 ❏ 藤崎 亮人 (a_fujisaki) ❏ @aki19035vc ❏ イタンジ株式会社 ❏
不動産会社向けのSaaSを提供 ❏ バックエンドエンジニア ❏ ほぼRails・たまにScala ❏ Next.js(Typescript)始めました ❏ 物件基盤の開発責任者 ❏ Rails × 型 の話が好き ❏ 学生の頃(2016年くらい)からEmacs使ってます
イントロ ❏ Railsで型を書くコツは最初から型を書いておくこと ❏ rails new と同時に型を導入する方法を紹介します ❏ サンプルのrailsアプリケーション ❏
https://github.com/aki19035vc/rails_with_rbs_202501 ❏ 各種バージョン(2025年1月時点で利用できる最新のもの) ❏ rails: 8.0.1 ❏ rbs: 3.8.1 ❏ rbs-inline: 0.10.0 ❏ rbs_rails: 0.12.1 ❏ steep: 1.9.3
rails new ❏ シンプルなAPIサーバー ❏ minimalオプションつけてもCIとかkamalとかsolid-xxxとかの設定ファイルが作 られる ❏ 不要なのでスキップ
scaffoldで最低限の体裁を整える ❏ scaffoldで最低限の体裁を整える ❏ migration, model, controller, routes, test とか全部作られる
型チェック用の gemを追加する ❏ Gemfileに3つのgemを追記してbundle install ❏ rbs-inline ❏ rbs(rubyの型定義)をソースコード内にコメントでかけるようにする gem
❏ rbs_rails ❏ ActiveRecordで動的に定義される型を生成する gem ❏ steep ❏ 実際に型チェックするgem
gemの型をローカルにダウンロード ❏ init ❏ rbs_collection.yamlをプロジェクトルートに生成する ❏ 型情報のダウンロード先とか諸々の設定ファイル ❏ install ❏
使用しているgemをGemfileから読み取り、プロジェクトルートの .gem_rbs_colleciton ディレクトリにダウンロードする
steepの設定 ❏ プロジェクトルートにSteepfileが生成される ❏ たくさんコメントが書かれている(左)が、シンプル (右)な設定に書き換える
rbs_railsのrakeタスクを作成 ❏ ActiveRecordで動的に定義される型を生成するrakeタスクを定義 ❏ デフォルトは rbs_rails:all というタスク名 ❏ https://github.com/aki19035vc/rails_with_rbs_202501/blob/8ab28bb320d69daacc4cc3532257f5f09a 633608/lib/tasks/rbs.rake
❏ 型情報はsig/rbs_rails ディレクトリ以下に出力される ❏ https://github.com/aki19035vc/rails_with_rbs_202501/tree/main/sig/rbs_rails
rbs-inlineで型定義を生成 ❏ app ディレクトリ以下のrubyファイルを読み込み、型定義を生成する ❏ 通常は # rbs_inline: enabled (マジックコメント)が付与されたファイルが対象
❏ 出力先は sig/rbs_inline/app ディレクトリ ❏ https://github.com/aki19035vc/rails_with_rbs_202501/tree/main/sig/rbs_inline/app ❏ --opt-out オプションでマジックコメントを付与しなくてもよくなる
型チェック!! ❏ 型の導入は完了 ❏ コントローラーの型を書いてないので書く必要あり ❏ モデルには何もメソッドを実装していないので書く必要なし
コントローラーに型を書く ❏ メソッドの上にコメントで型定義を記述 ❏ インスタンス変数 @posts @post にも型を 書く https://github.com/aki19035vc/rails_with_rbs_202501/blob/main
/app/controllers/posts_controller.rb
型チェック用の rakeタスクを用意 rails typecheck というコマンド一つで型 チェックを実行できるようにする ❏ rbs-inline・rbs_rails での生成 ❏
steep check を実行 https://github.com/aki19035vc/rails_with_rbs_202501/ blob/main/lib/tasks/rbs.rake
型チェック & カバレッジ
まとめ ❏ Railsで型を書くコツは最初から型を書いておくこと ❏ ミニマムな状態からなら型の導入はすぐできる ❏ rbs-inline, rbs_rails, steep という3つのgemを入れて少し設定するだけ