Skip to content

Instantly share code, notes, and snippets.

@bmizerany
Created January 19, 2011 01:43
Show Gist options
  • Save bmizerany/785530 to your computer and use it in GitHub Desktop.
Save bmizerany/785530 to your computer and use it in GitHub Desktop.
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"]
@ismasan
Copy link

ismasan commented Jan 19, 2011

By "foo" you mean "doit", right?

@philc
Copy link

philc commented Jan 19, 2011

It's clever and feels like fork. What's the alternative look like side by side?

@mloughran
Copy link

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