Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
最近追加した型の紹介とその振り返り
Search
a_fujisaki
July 11, 2024
Programming
920
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
63
rails newと同時に型を書く
aki19035vc
6
1.3k
“Rails × 型”での2年間を振り返る
aki19035vc
2
1.2k
rbs-inlineを使ってみた
aki19035vc
0
1.1k
Emacs × LSP × Steep
aki19035vc
0
300
MiniMagickの型を生成したい
aki19035vc
0
180
Other Decks in Programming
See All in Programming
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
0
250
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
240
Domain-Driven Transformation
hschwentner
2
2.4k
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
130
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
200
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
2k
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.1k
AI Augmentation Techniques with Liberty
ivargrimstad
0
140
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
490
Kotlinの15年とServer-Side Kotlin
n_takehata
1
170
GraphQL の onError と Nullability のこれから
quramy
0
200
sbt 2.0: takeoff
eed3si9n
0
180
Featured
See All Featured
Chasing Engaging Ingredients in Design
codingconduct
0
380
The Cost Of JavaScript in 2023
addyosmani
55
10k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
590
Un-Boring Meetings
codingconduct
0
470
How STYLIGHT went responsive
nonsquared
100
6.3k
Design in an AI World
tapps
1
370
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
68k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Ruling the World: When Life Gets Gamed
codingconduct
0
430
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Building Better People: How to give real-time feedback that sticks.
wjessup
371
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はマージされたらすぐ使えるようになるため、コントリビュートの実 感は得やすい 自前で書いている型があれば積極的にコントリビュートしていきましょう!!