Created
July 29, 2011 21:08
-
-
Save craigmartin/1114742 to your computer and use it in GitHub Desktop.
blocks/Procs/lambdas/and Methods -my view
This file contains 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
* Blocks are simply Procs that can not be saved. | |
* Blocks should be used as a one-time solution. If 2 or more closures are being passed, use a Proc. | |
Use case examples: | |
1.) Block: method is breaking an object down into smaller pieces, and I want to let my users interact with these pieces. | |
2.) Block: I want to run multiple expressions atomically, eg: a database migration. | |
3.) Proc: I want to reuse a block of code multiple times. | |
4.) Proc: the method will have one of more callbacks. | |
lambdas | |
* Similar to Procs except: | |
1.) lambdas check the number of arguments passed. | |
2.) lambdas return their value to the method and let the method continue on. | |
* lambdas are another way to write methods anonymously. | |
method Method | |
We can convert a Method object to a method and it will act just like a lambda except that it will be a named method vs lambdas being anonymous methods. | |
Main Point: | |
blocks and Procs act like drop-in code snippets. | |
lambdas and Methods act like methods. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment