Procs and lambdas

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…


Very 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 –> a nameless or anonymous block of code that can be represented as an object, can be passed around and called at will


class Array

def random_each(&b)  # => with the ampersand, b essentially becomes a reference to the code block below.

shuffle.each do |el| el  #=> b is a proc




[1,2,3,4,5].random_each do |el|

puts |el|


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.


Def run_two_procs (a, b)


proc1 = do

puts “This is proc1”


proc2 = do

puts “This is proc2”


run_two_procs proc1, proc2

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 without giving it a code block to assign to that Proc, 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. wants a block passed directly or it implicitly picks it up from the current scope/environment.

Def run_block

p = # **


run_block do

puts “Hello World’


In Ruby 1.9 there are 4 main ways to run or call a block:

my_proc = do |a|

puts “This is a proc and #{a} was passed along to me” #call method that passes in 10

my_proc.(20) #this isn’t a typo, gets converted to a call

my_proc[30] #

my_proc === 40 # triple equal is called the case equality operator, it’s primary function is used in case statements

several = { |number| number > 3 && number < 8 }

none = { |number| number == 0 }

case number

when several

puts “several”

when none

puts “none”


#In this case the several proc is being run and if it returns true it runs the puts –> this abstracts everything away


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.



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.

def run_proc(p)


name = “Fred”

print_a_name = proc { puts name } # this name doesn’t exist in the definition above. This magic is called a closure. (23 Min)

run_proc print_a_name


def run_proc(p)


name = “Fred”

print_a_name = proc { puts name }

name = “John”

run_proc print_a_name # => now this will output John because it kept a reference to the local variable.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s