dry-monads

v1.8
  1. Introduction
  2. Getting started
  3. Maybe
  4. Result
  5. Do notation
  6. Try
  7. List
  8. Task
  9. Validated
  10. Case equality
  11. Tracing failures
  12. Pattern matching
  13. Unit

TOC

  1. Case equality
    1. Nested structures

Case equality

Case equality

Monads allow to use default ruby case operator for matching result:

case value
when Some(1), Some(2) then :one_or_two
when Some(3..5) then :three_to_five
else
  :something_else
end

You can use specific Failure options too:

case value
when Success then [:ok, value.value!]
when Failure(TimeoutError) then [:timeout]
when Failure(ConnectionClosed) then [:net_error]
when Failure then [:generic_error]
else
  raise "Unhandled case"
end

Nested structures

case value
when Success(None()) then :nothing
when Success(Some { |x| x > 10 }) then :something
when Success(Some) then :something_else
when Failure then :error
end