0% found this document useful (0 votes)
58 views10 pages

Ruby Blocks, Procs, and Closures: CSE 413 Autumn 2008

This document discusses blocks, procs, and closures in Ruby. It explains that blocks allow passing code to methods to be executed, like coroutines. Methods can yield to blocks to call the passed code. Blocks form closures that can access variables in the surrounding scope. Procs are objects representing blocks that can be passed around. Lambdas are a type of proc that behave more like methods, where a return only exits the lambda. Together, blocks, procs, and lambdas allow implementing functional programming patterns in Ruby.

Uploaded by

BradFanp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views10 pages

Ruby Blocks, Procs, and Closures: CSE 413 Autumn 2008

This document discusses blocks, procs, and closures in Ruby. It explains that blocks allow passing code to methods to be executed, like coroutines. Methods can yield to blocks to call the passed code. Blocks form closures that can access variables in the surrounding scope. Procs are objects representing blocks that can be passed around. Lambdas are a type of proc that behave more like methods, where a return only exits the lambda. Together, blocks, procs, and lambdas allow implementing functional programming patterns in Ruby.

Uploaded by

BradFanp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSE 413 Autumn 2008

Ruby Blocks, Procs,


and Closures

Bl k
Blocks

Recall that any method call can have a


trailing
g block,, which can be executed byy
the method (almost like a coroutine)
all_words
_
=
words.each { | w | all_words = all_words + w + }

Bl k E
Block
Execution
ti

A block is executed in the context of the


method call.
Implications:

Access to variables at the call


location; return from a block returns from
surrounding method
def search(it, words)
words.each { | w | if it == w return }
puts not found
end

yield
i ld

Any method call can include a trailing


block. A method calls the block with a
yield statement.
def repeat
p
yield
yield
end
repeat { puts hello }

Output:
p
hello
hello

yield
i ld with
ith arguments
t

If the block has parameters, you can


provide expressions
p
p
with yyield to p
pass
arguments
def xvii
yield 17
end
xvii { | n | puts n+1 }
This

is exactly what an iterator does

Bl k and
Blocks
d Procs
P

Blocks (and methods) are not objects in Ruby


i.e., not things that can be passed around
as first-class values
But we can create a Proc object from a block
Procs

are closures consisting


g of the block and the
surrounding environment
Variations: procs and lambdas; slightly different
behavior
Several different ways to construct these; see the
language documentation for details

M ki Procs
Making
P

In a method, can have a parameter that


explicitly
p
y represents
p
the block
def return_a_block (& block)
block.call(17)
return block
end
The

& turns the block into a proc object


Proc objects support a call method

P
Proc.new;
l bd
lambdas

Can also create a proc object explicitly


p = Proc.new { | x, y | x+yy }

p.call(x,y)

The kernels lambda method also creates


j
proc objects
is_positive = lambda {|x| x > 0 }

P
Procs
vs. Lambdas
L bd

A Proc is a block wrapped in an object


and behaves jjust like a block
In

particular, a return in a Proc will return from


the surrounding
g method where the Procs
closure was created

Error if that method has already terminated

A Lambda is more like a method


Return

jjust exits from the lambda

Functional Programming in
Ruby
Ruby is definitely not a functional
programming language, but with blocks,
procs, and lambdas, you can do most
anything you could in a functional
lang age
language
For a good discussion, see ch. 6 in The
Ruby Programming Language by
Flanagan and Matsumoto

You might also like