I made a small utility library named
Wormhole.
The library enables us to communicate between caller and callee.
Here is a simple example of use.
ruby>>
require 'rubygems'
require 'wormhole'
def foo(array)
array << :foo # (1)
Wormhole.throw array
array << :baz # (3)
end
result = Wormhole.catch do
foo []
end.return do |array|
array << :bar # (2)
end
puts result.inspect # => [:foo, :bar, :baz]
<<--
First, the block passed to Wormhole.catch, the caller, is evaluated.
The foo method, the callee, throw an array object passing through the wormhole.
Then the array is caught by the last block passed to a return method. A block parameter of the block is the array.
Finally, the process goes back to the point 3 after the last block ends.
A return value of the foo method is returned via the return method.
By using this utility, you can participate to a depth of a complicated system from a safer position.
At the end, you can install this utility from the GitHub by using gem command like this.
pre>>
% sudo gem sources -a http://gems.github.com
% sudo gem install genki-wormhole
<<--
Have fun!
<pre> def foo(array)
array << :foo
yield(array)
array << :baz
end
foo do |array|
array << :bar
end</pre>
caller -> blackbox -> callee
The wormhole enable us to pass anything through the black box to callee from caller without any modifications to the black box.
Vice-versa.
caller <- blackbox <- callee
For example, the black box tends to be a framework such as Ruby on Rails, the callee is a callback and the caller is your application code.