Created
August 12, 2014 14:21
-
-
Save matsumonkie/18efe82d42424006698c to your computer and use it in GitHub Desktop.
return value method callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Result | |
def self.success | |
Result.new | |
end | |
def self.error(error) | |
Result.new(error) | |
end | |
def initialize(error = nil) | |
@error = error | |
end | |
def on_success | |
yield unless @error | |
end | |
def on_error | |
yield(@error) if @error | |
end | |
end | |
def method_that_computes_something | |
# do some stuff | |
#yield Result.error("bwaahh") | |
yield Result.success | |
end | |
def main | |
method_that_computes_something do |status| | |
status.on_success do | |
puts 'success' | |
end | |
status.on_error do |error| | |
puts "error: #{error}" | |
end | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment