Skip to content

Instantly share code, notes, and snippets.

@matsumonkie
Created August 12, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsumonkie/18efe82d42424006698c to your computer and use it in GitHub Desktop.
Save matsumonkie/18efe82d42424006698c to your computer and use it in GitHub Desktop.
return value method callback
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