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
最近追加した型の紹介とその振り返り
Search
a_fujisaki
July 11, 2024
Programming
880
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
最近追加した型の紹介とその振り返り
a_fujisaki
July 11, 2024
More Decks by a_fujisaki
See All by a_fujisaki
自作マクロと型生成
aki19035vc
0
52
rails newと同時に型を書く
aki19035vc
6
1.2k
“Rails × 型”での2年間を振り返る
aki19035vc
2
1.2k
rbs-inlineを使ってみた
aki19035vc
0
1k
Emacs × LSP × Steep
aki19035vc
0
290
MiniMagickの型を生成したい
aki19035vc
0
150
Other Decks in Programming
See All in Programming
The NotImplementedError Problem in Ruby
koic
1
870
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
720
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
210
RTSPクライアントを自作してみた話
simotin13
0
620
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
210
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
11
4.3k
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
130
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
600
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
210
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
200
Why Our Code Smells
bkeepers
PRO
340
58k
Navigating Weather and Climate Data
rabernat
0
230
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
600
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
560
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
150
Code Reviewing Like a Champion
maltzj
528
40k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Transcript
最近追加した型の紹介とその振り返り Roppongi.rb#20 2024/07/11 @aki19035vc
自己紹介 ❏ 藤崎 亮人 ❏ @aki19035vc ❏ 所属: イタンジ株式会社 ❏
バックエンドエンジニア ❏ Ruby歴5年くらい。Railsも同じくらい ❏ 学生の頃からEmacs使ってます
話す事 ❏ ここ最近で追加したり修正したりした型の話 (※ 事例紹介的な感じ) ❏ その際に困った事とか今後やりたい事なども併せて紹介 ※型についての基本的な解説はしません。 「ruby rbs
入門」とかで調べるとたくさん出てきます
コントリビュートのきっかけ ❏ 業務で書いているRailsアプリケーションでは型をほぼ100% 書いている ❏ gemの型はない事が多いため、使用するメソッドの型などは 自前で書く事が多い ❏ 他のRailsアプリケーションでも使いたい事もある ❏
せっかくなのでコミュニティに還元していきたい
PR作る前に何をしたか ❏ CONTRIBUTING.md を読む ❏ 既存の型定義を読む ❏ 他の人の過去のPRを読む ❏ gemのドキュメント・実装を読む
❏ ここが一番大変 ❏ 業務ではその時使用する特定の状態のみを記述して、とりあえず型チェックを 通すようにしていた ❏ コントリビュートする際は取りうる状態をすべて考慮して型を追加しないといけな い
最近やったこと ❏ csv: CSV::Row#initialize の型の追加 ❏ https://github.com/ruby/rbs/pull/1903 (まだマージされてません) ❏ zlib:
GzipReader のシングルトンメソッドの型の追加 ❏ https://github.com/ruby/rbs/pull/1911 (まだマージされてません) ❏ ActiveModel: バリデーション周りの型を追加・修正 ❏ https://github.com/ruby/gem_rbs_collection/pull/614 ❏ https://github.com/ruby/gem_rbs_collection/pull/615 ❏ MiniMagick: 新しく型を追加 ❏ https://github.com/ruby/gem_rbs_collection/pull/616
ActiveModelのバリデーション周りの型を追加・修正
ActiveModelのバリデーション周りの型を追加・修正 ❏ Rails 6.1 から追加されたActiveModel::Errorの型がないので追加した person = Person.new person.valid? person.errors.each
do |error| error #=> ActiveModel::Error end require "active_model" class Person include ActiveModel::Validations attr_accessor :name validates :name, presence: true end
困ったこと ❏ 7.0のディレクトリ内にある型ファイルが 6.0のディレクトリ内の型ファイルへの シンボリックリンクになっている ❏ 7.0の方にだけ型を追加したり修正したりする場合、既存の型定義を activemodel-6.0.rbs に移す必要がある gem_rbs_collection/gems/activemodel
├── 6.0 │ ├── activemodel-generated.rbs │ ├── activemodel.rbs │ └── patch.rbs └── 7.0 ├── activemodel-7.0.rbs ├── activemodel-generated.rbs -> ../6.0/activemodel-generated.rbs ├── activemodel.rbs -> ../6.0/activemodel.rbs └── patch.rbs -> ../6.0/patch.rbs
困ったこと ❏ 7.0のディレクトリ内にある型ファイルが 6.0のディレクトリ内の型ファイルへの シンボリックリンクになっている ❏ 7.0の方にだけ型を追加する場合、既存の型定義を activemodel-6.0.rbsに移す 必要がある gem_rbs_collection/gems/activemodel
├── 6.0 │ ├── activemodel-generated.rbs │ ├── activemodel.rbs │ └── patch.rbs └── 7.0 ├── activemodel-7.0.rbs ├── activemodel-generated.rbs -> ../6.0/activemodel-generated.rbs ├── activemodel.rbs -> ../6.0/activemodel.rbs └── patch.rbs -> ../6.0/patch.rbs ❏ 今回は既存の型は変更せず、ActiveModel::Errorをactivemodel-7.0.rbsに追加する だけにした ❏ シンボリックリンクを使う方法はどうにかしたいとのことなので、今後改善されるかも?
MiniMagickの型を追加
MiniMagick ❏ ImageMagickという画像を扱うソフトウェアのRubyバインディング ❏ 画像をリサイズしたりフォーマットを変換したりできる $ magick mogrify -resize 100x100
-format png -write output.png input.jpg require "mini_magick" image = MiniMagick::Image.open("input.jpg") image.resize "100x100" image.format "png" image.write "output.png"
困った事 ❏ method_missing で黒魔術してる ❏ ImageMagickのコマンドを作るDSLが提供されているイメージ ❏ 使用可能なオプションが何百と存在するため、それら全てをメソッドとして定義する のは大変 module
MiniMagick class Tool def method_missing(name, *args) option = "-#{name.to_s.tr('_', '-')}" self << option self.merge!(args) self end end end image.resize "100x100" image.format "png" image.write "output.png" #=> これらのメソッドは定義されていない
どうしたか・どうしたいか ❏ 実際に定義されているメソッドの型はひとまず追加 ❏ 定義されていないが、READMEで使われている一般的なメソッドは追加 ❏ ImageMagickで使えるオプションを全て定義することはできていない ❏ http://www.imagemagick.org/script/mogrify.php ドキュメントをパースして型定
義を生成したい
最後に ❏ 既存のgemの型の修正は意外と簡単 ❏ 新規にgemの型を追加する際は考える事が多くて意外と大変 ❏ rbs_collectionはマージされたらすぐ使えるようになるため、コントリビュートの実 感は得やすい 自前で書いている型があれば積極的にコントリビュートしていきましょう!!