Skip to content

Instantly share code, notes, and snippets.

@lpil
Last active April 22, 2017 11:14
Show Gist options
  • Save lpil/20443e197ee4786cc17387daebbec7ae to your computer and use it in GitHub Desktop.
Save lpil/20443e197ee4786cc17387daebbec7ae to your computer and use it in GitHub Desktop.
Examples of Ruby Result monad in use
# Given methods that return Results...
get_params
.and_then(method(:validate_params))
.and_then(method(:save_record))
.and_then(method(:send_email))
.match(ok: method(:render_success),
error: method(:render_failure))
# Given objects/classes that have a `.call` method which return Results...
[Result.ok(initial_state),
CheckVideoSize,
CheckVideoFrameRate,
CheckVideoFreezeDuration,
CheckVideoSilenceDuration,
SendToVideoService]
.reduce(:and_then)
# This array can be built dynamically. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment