Recent Posts

Railway Oriented programming in Ruby: do notation vs dry-transaction

7 minute read

Railway oriented programming is a design pattern which helps us handle errors in our applications. Instead of relying on exceptions, we design our data and functions in a specific way. Since applications are essentially just a combination of steps, we’ll make some design decisions about those steps and their structure:

  • There is a Result type, which can be either a Success or a Failure
  • Success and Failure are practically containers with different data
  • Steps accept Result and return Result
  • Once a step returns Failure, we stop further execution

I want to emphasize that Result is just an alternative name for the Either monad. Railway Oriented Programming comes from functional programming, so it is tightly related to the usual FP concepts like monads, composition, and many others. However, you don’t need to have an extensive knowledge of monads to use ROP in your code. In this article, I’ll show you how to write railway-oriented code in Ruby.