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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Shogo Takasaki
February 15, 2025
Programming
820
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rubyで始める関数型ドメインモデリング
Shogo Takasaki
February 15, 2025
Transcript
13 slides
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クラスを返すことがある程度担保されているので、エラーパターンの把握が容 易。
まとめ • 部分的に関数型のアプローチを取り入れることで、設計や実装時に立ち返る指針 が出来た • それによりメンテナビリティ、テスタビリティが向上した
Other Decks in Programming
See All in Programming
Skills Are the New npm [AAIF Bengaluru, Nov 2026]
shivamsaraswat
0
130
AIコーディングをセッションで終わらせない: PHPライブラリ開発におけるコンテキスト承継設計
ickx
0
110
AppFunctionsの内部実装を紐解く
hiroaki404
1
120
protojson における文字化けの扱い
kazuki1023
0
460
はじめてのKubernetesコントリビューション
nasa9084
0
320
Math.sumPreciseから学ぶ、ECMAScript仕様の数値の種類
syumai
0
290
Agentic AI with the Power of Jakarta EE
ivargrimstad
0
200
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
180
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
Promiseの向こう側へコンテキストを届ける ― proposal-async-context
seriseri
0
160
高橋メソッドでCVE解説シリーズ 〜CVE-2026-112816〜
hakkadaikon
0
280
Getting Started with Vibe Coding
miccheng
0
250
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
650
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
610
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
Design in an AI World
tapps
1
400
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.2k
[SF Ruby Conf 2025] Rails X
palkan
3
1.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
How to Talk to Developers About Accessibility
jct
2
630
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
2
160