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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
a_fujisaki
January 09, 2025
Programming
6
1.2k
rails newと同時に型を書く
a_fujisaki
January 09, 2025
Tweet
Share
More Decks by a_fujisaki
See All by a_fujisaki
自作マクロと型生成
aki19035vc
0
39
“Rails × 型”での2年間を振り返る
aki19035vc
2
1.2k
rbs-inlineを使ってみた
aki19035vc
0
930
Emacs × LSP × Steep
aki19035vc
0
260
MiniMagickの型を生成したい
aki19035vc
0
130
最近追加した型の紹介とその振り返り
aki19035vc
0
790
Other Decks in Programming
See All in Programming
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
200
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
190
Rで始めるML・LLM活用入門
wakamatsu_takumu
0
150
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
230
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
0
110
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
3.1k
DSPy入門 Pythonで実現する自動プロンプト最適化 〜人手によるプロンプト調整からの卒業〜
seaturt1e
1
470
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.4k
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
220
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
490
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
300
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
97
KATA
mclloyd
PRO
35
15k
RailsConf 2023
tenderlove
30
1.4k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
130
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
My Coaching Mixtape
mlcsv
0
63
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
450
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
280
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
93
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を入れて少し設定するだけ