Skip to content

Instantly share code, notes, and snippets.

@samwgoldman samwgoldman/result.rb
Last active Aug 29, 2015

Embed
What would you like to do?
success/failure result object
module Result
class << self
def success(value)
Success.new(value)
end
def failure(error)
Failure.new(error)
end
end
class Failure
def initialize(error)
@error = error
end
def flat_map
self
end
def map
self
end
def or_else
yield
end
def get_or_else
yield
end
def get!
raise @error
end
def for_each
end
end
class Success
def initialize(value)
@value = value
end
def flat_map
yield @value
end
def map
Success.new(yield @value)
end
def or_else
self
end
def get_or_else
@value
end
def get!
@value
end
def for_each(&block)
flat_map(&block)
nil
end
end
private_constant :Success, :Failure
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.