Method objects are created by Object#method, and are associated with a particular object (not just with a class). They may be used to invoke the method within the object, and as a block associated with an iterator. They may also be unbound from one object (creating an UnboundMethod) and bound to another.

class Thing
  def square(n)
thing =
meth  = thing.method(:square)                 #=> 81
[ 1, 2, 3 ].collect(&meth)   #=> [1, 4, 9]

[ 1, 2, 3 ].each(&method(:puts)) #=> prints 1, 2, 3

require 'date'
%w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
#=> [#<Date: 2017-03-01 ((2457814j,0s,0n),+0s,2299161j)>, #<Date: 2017-03-02 ((2457815j,0s,0n),+0s,2299161j)>]

Method Reference


Ruby supports two forms of objectified methods. Class Method is used to represent methods that are associated with a particular object: these method objects are bound to that object. Bound method objects for an object can be created using Object#method.

Ruby also supports unbound methods; methods objects that are not associated with a particular object. These can be created either by calling Module#instance_method or by calling unbind on a bound method object. The result of both of these is an UnboundMethod object.

Unbound methods can only be called after they are bound to an object. That object must be a kind_of? the method’s original class.

class Square
  def area
    @side * @side
  def initialize(side)
    @side = side

area_un = Square.instance_method(:area)

s =
area = area_un.bind(s)   #=> 144

Unbound methods are a reference to the method at the time it was objectified: subsequent changes to the underlying class will not affect the unbound method.

class Test
  def test
um = Test.instance_method(:test)
class Test
  def test
t =
t.test            #=> :modified
um.bind(t).call   #=> :original

UnboundMethod Reference


A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.

square = {|x| x**2 }  #=> 9
# shorthands:
square.(3)      #=> 9
square[3]       #=> 9

Proc objects are closures, meaning they remember and can use the entire context in which they were created.

def gen_times(factor) {|n| n*factor } # remembers the value of factor at the moment of creation

times3 = gen_times(3)
times5 = gen_times(5)               #=> 36                #=> 25   #=> 60


There are several methods to create a Proc

  • Use the Proc class constructor:

    proc1 = {|x| x**2 }
  • Use the Kernel#proc method as a shorthand of

    proc2 = proc {|x| x**2 }
  • Receiving a block of code into proc argument (note the &):

    def make_proc(&block)
    proc3 = make_proc {|x| x**2 }
  • Construct a proc with lambda semantics using the Kernel#lambda method (see below for explanations about lambdas):

    lambda1 = lambda {|x| x**2 }
  • Use the Lambda literal syntax (also constructs a proc with lambda semantics):

    lambda2 = ->(x) { x**2 }

Lambda and non-lambda semantics

Procs are coming in two flavors: lambda and non-lambda (regular procs). Differences are:

  • In lambdas, return means exit from this lambda;
  • In regular procs, return means exit from embracing method (and will throw LocalJumpError if invoked outside the method);

  • In lambdas, arguments are treated in the same way as in methods: strict, with ArgumentError for mismatching argument number, and no additional argument processing;

  • Regular procs accept arguments more generously: missing arguments are filled with nil, single Array arguments are deconstructed if the proc has multiple arguments, and there is no error raised on extra arguments.


p = proc {|x, y| "x=#{x}, y=#{y}" }, 2)      #=> "x=1, y=2"[1, 2])    #=> "x=1, y=2", array deconstructed, 2, 8)   #=> "x=1, y=2", extra argument discarded         #=> "x=1, y=", nil substituted instead of error

l = lambda {|x, y| "x=#{x}, y=#{y}" }, 2)      #=> "x=1, y=2"[1, 2])    # ArgumentError: wrong number of arguments (given 1, expected 2), 2, 8)   # ArgumentError: wrong number of arguments (given 3, expected 2)         # ArgumentError: wrong number of arguments (given 1, expected 2)

def test_return
  -> { return 3 }.call      # just returns from lambda into method body
  proc { return 4 }.call    # returns from method
  return 5

test_return # => 4, return from proc

Lambdas are useful as self-sufficient functions, in particular useful as arguments to higher-order functions, behaving exactly like Ruby methods.

Procs are useful for implementing iterators:

def test
  [[1, 2], [3, 4], [5, 6]].map {|a, b| return a if a + b > 10 }
                            #  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Inside map, the block of code is treated as a regular (non-lambda) proc, which means that the internal arrays will be deconstructed to pairs of arguments, and return will exit from the method test. That would not be possible with a stricter lambda.

You can tell a lambda from a regular proc by using the #lambda? instance method.

Lambda semantics is typically preserved during the proc lifetime, including &-deconstruction to a block of code:

p = proc {|x, y| x }
l = lambda {|x, y| x }
[[1, 2], [3, 4]].map(&p) #=> [1, 2]
[[1, 2], [3, 4]].map(&l) # ArgumentError: wrong number of arguments (given 1, expected 2)

The only exception is dynamic method definition: even if defined by passing a non-lambda proc, methods still have normal semantics of argument checking.

class C
  define_method(:e, &proc {})
end,2)       #=> ArgumentError   #=> true

This exception ensures that methods never have unusual argument passing conventions, and makes it easy to have wrappers defining methods that behave as usual.

class C
  def self.def2(name, &body)
    define_method(name, &body)

  def2(:f) {}
end,2)       #=> ArgumentError

The wrapper def2 receives body as a non-lambda proc, yet defines a method which has normal semantics.

Conversion of other objects to procs

Any object that implements the to_proc method can be converted into a proc by the & operator, and therefore con be consumed by iterators.

class Greater
  def initialize(greating)
    @greating = greating

  def to_proc
    proc {|name| "#{@greating}, #{name}!" }

hi ="Hi")
hey ="Hey")
["Bob", "Jane"].map(&hi)    #=> ["Hi, Bob!", "Hi, Jane!"]
["Bob", "Jane"].map(&hey)   #=> ["Hey, Bob!", "Hey, Jane!"]

Of the Ruby core classes, this method is implemented by Symbol, Method, and Hash.           #=> "1"
[1, 2].map(&:to_s)              #=> ["1", "2"]

method(:puts)   # prints 1
[1, 2].each(&method(:puts))     # prints 1, 2

{test: 1}       #=> 1
%i[test many keys].map(&{test: 1})  #=> [1, nil, nil]

Proc Reference