This site is a preview of the curriculum for our Summer 2018 bootcamp in Burlington, Vermont. As we continue our preparation, courses and lessons will appear and disappear; we will rename, rearrange, clarify, and obfuscate as needed... Please consider this a work in progress and keep checking in.

Ruby Blocks:


Code Blocks

Ref. WGR Section 6.3, "Iterators and code blocks"

What is a block?

a block is a chunk of code

the term "block" overlaps with the terms...

closure, proc, lambda, function, function pointer, anonymous function, callback, runnable, functor, delegate

method vs. block vs. proc

  • a method is a chunk of code starting with def
  • a block is a chunk of code starting with do
  • a proc is an object that points to a block

(Of these three, only a proc can be stored in a variable or named parameter.)

How do you declare a block?

  • Anywhere you see "do" in Ruby, it's the start of a block

    3.times do
      puts "Hip! Hip! Hooray!"
  • Blocks can also be wrapped in curly braces

    3.times { puts "Hip! Hip! Hooray!" }
  • braces are for a single line, do...end for multiple lines

What are blocks for?

A block is a piece of code that is declared but not run in the place it's written. The idea is to leave it up to the receiver of the block to decide when to call it. -- Wolfram Arnold

So you use blocks for...

  • stashing away some code to be run later
    • callbacks
    • initializers
    • asynchronous IO
  • separating the body of a loop from the loop itself
    • iterators
  • running some extra code before and/or after
  • making your code look cool

The "Hole In The Middle" Pattern

  • Imagine two very similar algorithms
    • capitalize each string in an array
    • reverse each string in an array
  • Now imagine extracting the common parts into a single function...
    • ...with a hole in the middle
  • The block you write fills that hole


  • The proc keyword defines a block
  • You can store that block into a variable
  • You can call that block with the call method
say_hi = proc { puts "hi" }  # prints "hi\n"

procs can take parameters too

capitalize_it = proc { |word| word.capitalize }"banana")   #=> "Banana""cherry")   #=> "Cherry"

Passing Blocks to Methods Explicitly with Procs

twice is a less cool version of times that takes a proc parameter

def twice(action)

You can assign a proc to a variable and pass it as a parameter

say_hi = proc do
  puts "hi!"
twice(say_hi)  # prints "hi!\n" twice

You can also define proc inline rather than assigning it to a variable

twice(proc do
  puts "hi!"
end)  # prints "hi!\n" twice

The Default Block

  • Every method, no matter what its parameter list, might get an optional magic invisible block parameter
  • This is called "the default block" and the method can call it using yield


Passing Blocks to Methods Implicitly with the Default Block

twice is a less cool version of times that takes a default block (invisible parameter)

def twice

twice do
  puts "hi!"

"twice do" kind of almost resembles English a little, right?

yield turns prose into poetry

Which is more beautiful?

Using procs:

def twice block

twice(proc { puts "hi" })

Using the default block:

def twice

twice { puts "hi" }

The default block can accept parameters

def twice
  yield 0
  yield 1

twice do |i|
  puts "#{i+1} Mississippi"


1 Mississippi
2 Mississippi

Passing Blocks Implicitly

for_each is a less cool version of Array.each

def for_each(array)
  i = 0
  while i < array.size
    yield array[i]
    i += 1

names = ["alice", "bob", "charlie"]
for_each(names) do |item|
  puts "hi, #{item}"
  • array is a parameter to the for_each function
  • item is a parameter to the block

Blocks can also return values

map_it is a less cool version of

def map_it(array)
  i = 0
  out = []
  while i < array.size
    out << yield(array[i])
    i += 1

names = ["alice", "bob", "charlie"]
map_it(names) do |item|
#=> ["ecila", "bob", "eilrahc"]


  • block_given? is true if a block was passed
  • common use:

    yield if block_given?

Making the default block visible

& turns the default block into a proc

def for_each(array, &p)
  i = 0
  while i < array.size[i])
    i += 1

a = ["alice", "bob", "charlie"]
for_each(a) do |item|
  puts "hi, #{item}"

You can also use & in the caller

turns a proc into a default block

capitalize_word = proc {|w|w.capitalize}


Ruby also has a keyword lambda that works just like proc, except for a few technical details.

  • a lambda enforces arity (the number of parameters)
  • in a lambda, return exits the block, not the enclosing method

proc is easier to use but lambda is more nerdy


comments powered by Disqus