Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby de Railway Oriented Programming

Avatar for wtnabe wtnabe
August 16, 2025

Ruby de Railway Oriented Programming

dry-operationを使ったRubyでのRailway Oriented Programmingの紹介。

Kanazawa.rb meetup #156 at ビブリオバウムでの発表です。

Avatar for wtnabe

wtnabe

August 16, 2025
Tweet

More Decks by wtnabe

Other Decks in Programming

Transcript

  1. Railway Oriented Programming | F# for fun and profit 2014

    年のアイディア(別に新しくない) 実態は Either を利用したエラーハンドリングのレシピ モナドに深入りするつもりはなく、これが唯一の方法でもない ドメインエラー(ドメインモデルのドメイン)で使うのがよい
  2. F# の記述例 before bind nameNotBlank bind name50 bind emailNotBlank after

    bind nameNotBlank >> bind name50 >> bind emailNotBlank
  3. dry-operation を使ったRuby の記述例 class UpdateCustomer < Dry::Operation def call step

    name_not_blank step name_50 step email_not_blank end end UpdateCustomer.new.call # => Dry::Monads::Result
  4. 以上を踏まえてもう一度コード class SomeOperation < Dry::Operation def call(args) val1 = step

    <Resultを返すメソッド> val2 = step <Resultを返すメソッド> step <Resultを返すメソッド> end end SomeOperation.new.call(args) # => Dry::Monads::Result
  5. dry-operation は何をしているか call の中身は別なmodule になる call はDry::Monads::Result を返す step はDry::Monads::Result

    を受け取ってunwrap して返す Result がFailure だったら例外を使って即脱出する 脱出時にはFailure を返す
  6. Dry::Monads::Result とは dry-rb - dry-monads v1.6 - Result Success オブジェクトあるいはFailure

    オブジェクトのこと 値やメッセージの意味を明確にするためのオブジェクト(型) Rust とかあの辺りの流れ
  7. ROP に対する本人による5 年後の反論 Against Railway-Oriented Programming | F# for fun

    and profit なんでもかんでも Result にすべきでない Be careful when using Result for I/O errors などなど。まぁ最初から絶対の正解とは言ってない。
  8. Domain Modeling Made Functional における分類 Domain Errors ビジネスロジックであり、リッチなbool としてResult を使う

    Panics 例外でよい Infrastracture Errors ここが境界で、設計の腕の見せどころ Against Railway-Oriented Programming | F# for fun and profit
  9. 参考 Railway Oriented Programming | F# for fun and profit

    https://github.com/swlaschin/Railway-Oriented-Programming- Example Against Railway-Oriented Programming | F# for fun and profit Railway Oriented programming in Ruby: do notation vs dry- transaction | Igor Morozov dry-operation のススメとエラー情報をView まで持っていく方法の 模索 (2025-01-26) | あーありがち Result 型とRailway Oriented Programming をめぐる旅 (2025-02- 08) | あーありがち