In Ruby we create an anonymous chunk of code call
Proc.new. We call this using the
Think of proc as a “saved” block: just like you can give a bit of code a name and turn it into a method, you can name a block and turn it into a proc.
Lambdas appear to be the same, but lambdas are pickier about arguments. Can’t pass an extra arg to a lambda (it will blow up), while the proc will just throw away the extras.
Lambda return statement just exited execution of the statement itself. The proc ended execution of containing method.
There is an extra short form for lambdas called stabby lambdas.
As a rule, stick with lambdas until you find a specific reason not to.
Convenient to write methods that accepts a single proc as arugments. Callers can pass in whatever code they want to be executed once of each name. This method that is so common that has a single argument that is expected to be a proc is got it’s own name – a block. Every ruby method can implicity receive a proc arg with the yield keyword.
A method with a singe proc as as arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
You know how everything in Ruby is an object, well, as it turns out that’s not quite true. Ruby blocks are not objects! So, blocks are not objects, but you can turn them into objects without too much trouble. We do this by wrapping our block in an instance of the Proc class…
Procs and lambdas are basically blocks that have been assigned to variables.
- Advantages of procs over blocks:
- Procs are full-fledged objects, so they have all the powers and abilities of objects. (Blocks do not.)
- Unlike blocks, procs can be called over and over without rewriting them. This prevents you from having to retype the contents of your block every time you need to execute a particular bit of code.
- Need to pass a block in as a parameter when creating a block (even if it is an empty block)
1 2 3 4 5
- you call a proc with the method .call -> my_proc.call
1 2 3 4
It is standard to pass along a code block to the each method
block_given? –> method to check whether a block was given to a method or not
A proc is a nameless or anonymous block of code that can be represented as an object, can be passed around and called at will
1 2 3 4 5 6 7 8 9 10 11 12
You can only pass a single block to a method at one time, but you can pass multiple procs around because they are regular object like arrays or strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
If you have a code block, you can call it with yield, you can use the ampersand argument to convert it into a proc,
**if you use Proc.new without giving it a code block to assign to that Proc, Proc.new will look to see if the current scope has been passed a code block and then it will use that –> it will do it all implicitly, you don’t need to specify anything here. If it can’t find a block (e.g. if you commented the block below) it raises an error. Proc.new wants a block passed directly or it implicitly picks it up from the current scope/environment.
1 2 3 4 5 6 7 8
In Ruby 1.9 there are 4 main ways to run or call a block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
–>In this case the several proc is being run and if it returns true it runs the puts –> this abstracts everything away
Symbol to proc
- Create a method, greeter, that takes no arguments and yields to a block.
- Create a proc, phrase, that puts “Hello there!”. Pass this to greeter instead of a block. (Don’t forget to pass &phrase instead of just phrase!)
1 2 3 4 5 6 7 8 9
Can also use this to cast or convert types
1 2 3
1 2 3
Can’t assign variables to keywords
Lambdas are much more sensitive than Procs to parameters. Much like methods, they need to receive the exact number of arguments as defined. Procs will just grab nil if nothing has been passed in.
1 2 3 4 5
In ruby a closure is like an anonymous code block, but that maintains references to local variables and were being used at the time of the definition of that code.
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
Returns: - The lambda returns the last line of the method just like a regular method - A proc returns only what is executed INSIDE the proc.
SOME MORE READING:
- Know your closures
- Skorks- best explanation out there
- Learn to program- chapter 10
- Pragmatic Programmer Dave’s explanation