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
@JEG2, I thought to use only method name in my first start here:
https://gist.github.com/dd8f3d26546d506df6fe/a5838ff29238faa533158ae83d1c88f50abcb611
But later on I realize, I have to define those method(:success and :failure) in Proc class before hand, so I don't like that because it doesn't scale. After that I was thinking about using method_missing to get it supports that, but I feel not good about method_missing because it could make Proc class becomes messy. Finally, I found _(underscore) is valid syntax for method calls for minimun character and doesn't conflict with others, that's why I choose it.