Member-only story

Blocks, Procs, and Lambdas in Ruby

Mastering Ruby: A Deep Dive into Blocks, Procs, and Lambdas

Patrick Karsh
2 min readJan 3, 2024

Blocks, Procs, and Lambdas are fundamental concepts in Ruby, each providing unique ways to handle blocks of code. Understanding their differences and how they work is crucial for mastering Ruby programming.

Blocks

A block in Ruby is essentially an anonymous piece of code that can be passed to methods. Blocks can have their own parameters and are enclosed in do...end syntax or curly braces {}.

Example of a Block:

This block is passed to the each method and executed for each element in the array.

Procs

Procs (short for procedures) are objects that encapsulate a block of code. They can be stored in variables and passed around like any other object.

Creating and Using a Proc:

--

--

Patrick Karsh
Patrick Karsh

Written by Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh

No responses yet