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
Rubyで始める関数型ドメインモデリング
Search
Shogo Takasaki
February 15, 2025
Programming
790
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rubyで始める関数型ドメインモデリング
Shogo Takasaki
February 15, 2025
Other Decks in Programming
See All in Programming
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.7k
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
110
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
Railsバージョンを4倍にした話
optim
0
440
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
1.1k
カツオ、ご期待ください
suneo3476
0
190
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
430
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
2
410
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
Featured
See All Featured
Designing for humans not robots
tammielis
254
26k
Balancing Empowerment & Direction
lara
6
1.3k
Automating Front-end Workflow
addyosmani
1369
210k
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
A Soul's Torment
seathinner
8
3.8k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
How to make the Groovebox
asonas
2
2.5k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
630
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Transcript
Rubyで始める関数型ドメインモデリング 湘.なんか #2 @shogo_tksk / 2025/02/15
話しているひと • 高崎(@shogo_tksk) • 普段はRails/Reactを書いています • 一番好きなモールは湘南モールフィル🏝
今日話すこと • 関数型の思考を取り入れることで、より堅牢なコードをかける(かも?) • Rubyにおける実装例
モチベーション • 本書はF#で書かれているが、その考え方自 体は言語仕様に依らず普遍的。 • そのエッセンスをRuby / Railsで書かれた コードに落とし込むとしたらどうか? 引用:
https://amzn.asia/d/7Lh0iN4
関数型プログラミングのエッセンスとは?🤔
不変性(Immutable) • オブジェクトを直接変更せず、新しいオブジェクトを生成して操作を行う • 意図せず他のオブジェクトの状態を壊さない
純粋関数 • 引数が同じ場合、常に同じ値を返す(参照透過性) • 副作用を持たない(関数の場合は「値を返す」ことが主たる作用) 引数以外の状態に依存
None
Railsにおける典型的な副作用(I/O) 不純な世界(副作用)と計算を分離する I/O境界 I/O境界 👈ココを純粋に保ちたい
引用: https://zenn.dev/coconala/articles/2a885527bf2f32 ◦ dry-monadsというGemを使った例 ◦ 処理のステップ1つ1つを純粋関数にする ▪ 副作用を避けるため、例外を吐かない。大 域脱出もしない。 ▪
その代わりにResultクラス(Success / Failure)を返す ◦ そのステップを組み合わせることで、大きな処理 (ワークフロー)を作る 👈 ドメインロジックをそのまま書く
引用: https://gitlab.com/gitlab-org/gitlab/-/blob/d8b87be14c3c27eb7cfef1bc502d9cc4e5c8ff0f/app/services/service_response.rb • 独自でResultクラスを定義するパターン • Gem依存を避けたい、欲しいのは Resultク ラスのみで、多機能なのは too muchな場
合など • GitLabのコードでは独自に定義した Result を返すことでドメインロジック上の例外を表 現している
やってみて嬉しいポイント • 関数を純粋に保つことでテストが早い &書きやすい ◦ 副作用(I/O)とロジックを分離することで、純粋な計算として扱える。 ◦ 逆にテストが書きにくい場合は、責務の分割が適切じゃないかも?副作用を孕んでいない か?などを考えられるようになった。 ▪
よりよいコードを書くためにテストを書く • 各関数がResultクラスを返すことがある程度担保されているので、エラーパターンの把握が容 易。
まとめ • 部分的に関数型のアプローチを取り入れることで、設計や実装時に立ち返る指針 が出来た • それによりメンテナビリティ、テスタビリティが向上した