Skip to content

Instantly share code, notes, and snippets.

@kevbuchanan
Created February 25, 2015 15:27
Show Gist options
  • Save kevbuchanan/65a7297f5bb78a4bb114 to your computer and use it in GitHub Desktop.
Save kevbuchanan/65a7297f5bb78a4bb114 to your computer and use it in GitHub Desktop.
Result Object
class Result
def self.status(status, result)
new(status, result)
end
def initialize(status, result)
@status = status
@result = result
end
def on_status(status, &block)
if @status == status
Result.status(@status, block.call(result))
else
Result.status(@status, result)
end
end
end
result = Result.status(:not_found, nil)
title = result.on_status(:success) do |post|
post.title
end.on_status(:not_found) do
"Title not found"
end.result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment