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
0
620
最近追加した型の紹介とその振り返り
a_fujisaki
July 11, 2024
Tweet
Share
More Decks by a_fujisaki
See All by a_fujisaki
rails newと同時に型を書く
aki19035vc
6
990
“Rails × 型”での2年間を振り返る
aki19035vc
2
1.1k
rbs-inlineを使ってみた
aki19035vc
0
770
Emacs × LSP × Steep
aki19035vc
0
200
MiniMagickの型を生成したい
aki19035vc
0
120
Other Decks in Programming
See All in Programming
XP, Testing and ninja testing
m_seki
3
250
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
790
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
290
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
440
レベル1の開発生産性向上に取り組む − 日々の作業の効率化・自動化を通じた改善活動
kesoji
0
240
Is Xcode slowly dying out in 2025?
uetyo
1
280
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
490
テスト駆動Kaggle
isax1015
1
420
オンコール⼊⾨〜ページャーが鳴る前に、あなたが備えられること〜 / Before The Pager Rings
yktakaha4
0
120
AI時代の『改訂新版 良いコード/悪いコードで学ぶ設計入門』 / ai-good-code-bad-code
minodriven
20
8.1k
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
530
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
260
Featured
See All Featured
Speed Design
sergeychernyshev
32
1k
Producing Creativity
orderedlist
PRO
346
40k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.5k
GraphQLとの向き合い方2022年版
quramy
49
14k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
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はマージされたらすぐ使えるようになるため、コントリビュートの実 感は得やすい 自前で書いている型があれば積極的にコントリビュートしていきましょう!!