Railway-oriented operations for Ruby, in the spirit of Trailblazer, compiled to
plain Ruby. You declare steps; on first use Octorail turns them into one
ordinary method of ifs and returns, so a call costs about what
hand-written code costs and allocates no more.
It is deliberately strict: steps are instance methods, ctx is a Hash, the railway is linear, and anything ambiguous is an error at load time with the line that caused it.
Requires Ruby >= 3.3. No dependencies.
gem "octorail"class Orders::Checkout < Octorail::Operation
output :order
nested Orders::Validate
wrap :transaction do
step :create_order
nested Payments::Charge
end
switch :delivery_for do
on(:pickup)
on(:courier) { step :book_courier }
end
pass :send_receipt
failure :log_failure
def transaction(_ctx, &) = ActiveRecord::Base.transaction(requires_new: true) { yield || raise(ActiveRecord::Rollback) }
def create_order(ctx, user:, total:) = ctx[:order] = user.orders.create!(total:)
def delivery_for(_ctx, delivery: :courier) = delivery
def book_courier(ctx, order:, address:) = Couriers.book(order, address)
def send_receipt(_ctx, order:) = ReceiptMailer.with(order:).deliver_later
def log_failure(_ctx, error:, failed_step:) = Rails.logger.warn("checkout: #{error} at #{failed_step}")
end
result = Orders::Checkout.call(user:, total: 42, address: "Kyiv")
result.success? # => false
result.error # => :declined (from inside Payments::Charge)
result.failed_step # => :charge
case result
in { success: true, order: } then redirect_to order
in { error: :declined } then render :card_declined
endAnd when you need to know why:
Orders::Checkout 4.874 ms
├── ✓ nested Orders::Validate 0.636 ms
│ └── ✓ check 0.002 ms
├── wrap transaction → false 4.176 ms
│ ├── ✓ create_order 0.001 ms
│ └── ✗ nested Payments::Charge 4.171 ms
│ ├── ✓ authorize 0.001 ms
│ └── ✗ charge 2.993 ms
├── · failure log_failure 0.001 ms
└── failure error=:declined failed_step=:charge
step/pass/failure, with keyword arguments read from ctx and a clear error when a required key is missing.wrapfor transactions, locks, retries;nestedto compose operations;switchto branch on a value.fail_fast:andfailure ..., recover: trueto leave or rejoin the success track.- Results with
error,failed_step, declared outputs and pattern matching. - Tracing on demand (
call_traced), per operation or globally, attached to results and to exceptions. - Errors at load time that point at the declaration line.
Ten trivial steps, Ruby 4.0 with YJIT: Octorail 416 ns, a hand-written PORO 425 ns, Yaso 897 ns, Decouplio 1.1 µs, Trailblazer 25.2 µs. Allocations equal the PORO's. Averaged over 36 benchmarks (3 Rubies, with and without YJIT): 1.7× faster than Yaso, 1.9× than Decouplio, 38× than Trailblazer. Full tables and caveats in performance.
- Guide: one operation built end to end
- Steps · Result · Fail fast and recover
- Wrap · Nested · Switch · Inheritance and macros
- Compilation · Tracing · Configuration · Errors
- Testing · Performance
Each page ends with Limits: what the gem forbids around that feature, why, and what to do instead.
Octorail itself doesn't depend on Rails. To compile every operation at boot, so a broken one fails the deploy instead of the first request, add an initializer:
# config/initializers/octorail.rb
Rails.application.config.after_initialize do
Octorail.compile_all! if Rails.configuration.eager_load
endEverything runs in Docker; no local Ruby needed.
make build # build images for Ruby 3.3 / 3.4 / 4.0
make test # rspec on Ruby 3.3
make test RUBY=ruby40 # rspec on a specific Ruby
make test-all # rspec on every supported Ruby
make lint # rubocop
make bench RUBY=ruby34 # benchmarks against Yaso, Decouplio, Trailblazer
make console # IRB with the gem loadedMIT, see LICENSE.txt.
