MethodCallbacks helps you to pass multi blocks to a method, and lets the method calls yeild for each one. Let's see examples:
require_relative 'ext_proc.rb'
def sum(a, b, &block)
result = a + b
block.yield(:success, result)
rescue Exception => e
block.yield(:failure, e)
end
sum(3, 2) do
_(:success){ |result|
puts "Total sum is: #{result}"
}
_(:failure){ |exception|
# Never get calls because calculation went ok
}
end
sum('3', 2) do
_(:success){ |result|
# Never get calls because calculation went bad
}
_(:failure){ |exception|
puts "Something went wrong with calculation: #{exception.message}"
}
end
Like the simplicity!