Created
January 19, 2011 01:43
-
-
Save bmizerany/785530 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def doit(&blk) | |
if blk.arity == 2 | |
blk.call("mydata", false) | |
blk.call(nil, true) | |
else | |
blk.call("mydata") | |
end | |
end | |
doit {|data, done| p [data, done] } | |
# ["data", false] | |
# [nil, true] | |
doit {|data| p [data] } | |
# ["data"] |
It's clever and feels like fork. What's the alternative look like side by side?
It's compact but confusing as a pattern. Wouldn't it be better to use a deferrable for the 'doneness' and return some kind of event emitter so that you can attach callbacks to the other events you're interested in.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By "foo" you mean "doit", right?