Getting Closure
Ruby Blocks, Procs and Lambdas
Tuesday, May 28, 13
Slide 2
Slide 2 text
Johnny Boursiquot (boar-cee-co)
Technologist
Tech Director/Software Architect at a
local interactive agency (MAARK)
@jboursiquot / jboursiquot.com
Tuesday, May 28, 13
Slide 3
Slide 3 text
What we’ll talk about
Closures - What they are, their value
Blocks
Procs
Lambdas
Where to go (literally) from here
Tuesday, May 28, 13
Slide 4
Slide 4 text
So, what’s a “closure” anyway?
Tuesday, May 28, 13
Slide 5
Slide 5 text
A closure is...
A function or method that can be passed
around like an object
It remembers variables that were in
scope when it was created
Tuesday, May 28, 13
Slide 6
Slide 6 text
Let’s take a step back...
Tuesday, May 28, 13
Slide 7
Slide 7 text
December 2003
Tuesday, May 28, 13
Slide 8
Slide 8 text
Notable Events
Tuesday, May 28, 13
Slide 9
Slide 9 text
Barry White dies
Tuesday, May 28, 13
Slide 10
Slide 10 text
“Matz” has an interview...
Tuesday, May 28, 13
Slide 11
Slide 11 text
December 22, 2003
A Conversation with Yukihiro Matsumoto, Part III
http://www.artima.com/intv/closures.html
Tuesday, May 28, 13
Slide 12
Slide 12 text
“Blocks are basically nameless
functions...Basically, you can pass a
nameless function to another function, and
then that function can invoke the passed-in
nameless function.”
Tuesday, May 28, 13
Slide 13
Slide 13 text
“Many other languages do this style of
programming. In Ruby, the difference is
mainly a different kind of syntax...”
Tuesday, May 28, 13
Slide 14
Slide 14 text
Blocks
Tuesday, May 28, 13
Slide 15
Slide 15 text
Block Syntax
Tuesday, May 28, 13
Slide 16
Slide 16 text
Well, why is this important?
Tuesday, May 28, 13
Slide 17
Slide 17 text
Block Syntax
Sure, it works.
Looks familiar.
But, it’s full of coupling.
Tuesday, May 28, 13
Slide 18
Slide 18 text
Block Syntax
Block helps you decouple logic from iteration
They take parameters -- like methods
They appear after method invocation -- as if an
additional parameter to your method call
Tuesday, May 28, 13
Slide 19
Slide 19 text
Block Syntax
Block called once for each iteration of array
Each element passed into block as a variable
Variables defined inside block are local to that block
Tuesday, May 28, 13
Slide 20
Slide 20 text
Block Syntax
“yield” is used in methods that accept blocks as
parameters to transfer control to the block and back
Tuesday, May 28, 13
Slide 21
Slide 21 text
Block Rules
Parameters to a block are always local to that block,
even if variable names are shared with the outside.
Tuesday, May 28, 13
Slide 22
Slide 22 text
Block Rules
You can define block-local variables by putting
them after a semicolon in the block's parameter
list.
Tuesday, May 28, 13
Slide 23
Slide 23 text
Hmmm....
How can we not repeat ourselves whenever we need to
double a number?
Tuesday, May 28, 13
Slide 24
Slide 24 text
Back to Closures
Tuesday, May 28, 13
Slide 25
Slide 25 text
A closure is...
A function or method that can be passed
around like an object
It remembers variables that were in
scope when it was created
Tuesday, May 28, 13
Slide 26
Slide 26 text
Procs
Tuesday, May 28, 13
Slide 27
Slide 27 text
Proc Syntax
We turn the logic we need into a Proc so we can
pass it around
We use “&” when calling our collect method to
convert the Proc back into a block because it
expects one
Tuesday, May 28, 13
Slide 28
Slide 28 text
Proc’s Benefits
Unlike blocks, they are objects that can
be passed around
Unlike blocks, they can be called upon
multiple times, making our code DRYer
Tuesday, May 28, 13
Slide 29
Slide 29 text
Lambdas
Tuesday, May 28, 13
Slide 30
Slide 30 text
Lambda Syntax
Tuesday, May 28, 13
Slide 31
Slide 31 text
So...what’s the difference?
Tuesday, May 28, 13
Slide 32
Slide 32 text
Procs vs Lambdas
Lambdas check the number of arguments
passed in. Procs do not.
Procs ignore any missing arguments,
assigning nil to them.
Lamdas return to the calling method,
Procs return immediately without going
back to the caller.
Tuesday, May 28, 13
Slide 33
Slide 33 text
Behavior of Missing Arguments
Tuesday, May 28, 13
Slide 34
Slide 34 text
Behavior of Return Statements
Calling return from inside a proc will return from
calling method
Calling return from lambda will return only from the
lambda
Tuesday, May 28, 13
Slide 35
Slide 35 text
Not Covered in Depth
Tuesday, May 28, 13
Slide 36
Slide 36 text
Closure Scope Retention
Create a copy of all variables at time of
definition
Retain reference to variables (Ruby’s
uses this method)
Tuesday, May 28, 13
Slide 37
Slide 37 text
We’ll save that for a Project Night
1st Tuesdays of the Month
Tuesday, May 28, 13
Slide 38
Slide 38 text
Also...
Tuesday, May 28, 13
Slide 39
Slide 39 text
October 12-13, 2013
wickedgoodruby.com
Tuesday, May 28, 13