Ruby Language Blocks and Procs and Lambdas Objects as block arguments to methods

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Putting a & (ampersand) in front of an argument will pass it as the method's block. Objects will be converted to a Proc using the to_proc method.

class Greeter
  def to_proc
    Proc.new do |item|
      puts "Hello, #{item}"
    end
  end
end

greet = Greeter.new

%w(world life).each(&greet)

This is a common pattern in Ruby and many standard classes provide it.

For example, Symbols implement to_proc by sending themselves to the argument:

# Example implementation
class Symbol
  def to_proc
    Proc.new do |receiver|
      receiver.send self
    end
  end
end

This enables the useful &:symbol idiom, commonly used with Enumerable objects:

letter_counts = %w(just some words).map(&:length)  # [4, 4, 5]


Got any Ruby Language Question?