Ruby Blocks, Procs, and Lambdas Explained

Learn the practical differences between Ruby blocks, Procs, and lambdas, including argument handling, return behavior, and real usage patterns.

By Anderson Alvarez Vásquez · 2026-11-21

  • ruby
  • blocks
  • procs
  • lambda

Ruby’s blocks, Procs, and lambdas are part of what makes the language feel expressive. They let you pass behavior around, not just data. That power is useful, but it can also be confusing when you are not clear on how each one works. If you have ever wondered why a method accepts &block , why Proc.new behaves differently from lambda , or when to use yield instead of an explicit block parameter, this article focuses on that exact problem. The core idea In Ruby, you can pass a chunk of code into a method. That chunk can be written as: - an inline block - a Proc - a lambda They overlap, but they are not identical. The main differences are: - how arguments are checked - how return behaves - whether the object is strict about arity - whether the code is passed implicitly or explicitly Understanding these differences helps you write APIs that are easier to use and less surprising. 1 Blocks: the simplest form A block is the most common way to pass code into a method. ruby def greet puts "Hello" yield if block given? end greet do puts "Welcome to Ruby" end Output: ruby Hello Welcome to Ruby A block is not a full object by default. It is tied to the method call. You usually use it when a method needs to customize part of its behavior, like iterating, wrapping work, or formatting output. When blocks shine - iterating over collections - wrapping setup/teardown behavior - defining DSL-style APIs - keeping call sites readable A practical example ruby def with timing start = Time.now result = yield finish = Time.now puts "Elapsed: {finish - start} seconds" result end value = with timing do sleep 1 "done" end puts value Here, the method owns the structure, and the block supplies the custom operation. 2 Turning a block into an object with Proc A Proc is a block converted into an...