Ruby Academy · Lesson

Blocks, Procs, and Lambdas

Understand how Ruby handles anonymous functions using blocks, Procs, and Lambdas.

Lesson 2 of 310 steps

Blocks, Procs, and Lambdas is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 3 lessons in total.

Blocks, Procs, and Lambdas

Ruby provides powerful ways to handle reusable pieces of code using blocks, Procs, and Lambdas.

In this lesson, you will learn how to use these concepts effectively.

Blocks, Procs, and Lambdas — illustration 1

What is a Block?

A block is a piece of code enclosed between do ... end or curly braces { ... }.

[1, 2, 3].each do |num|
  puts num * 2
end

Single-Line Blocks

For simple operations, you can use curly braces {} instead of do ... end.

[1, 2, 3].each { |num| puts num * 2 }

Passing Blocks to Methods

Blocks can be passed to methods like each, map, and times.

5.times { puts 'Hello, Ruby!' }

What is a Proc?

A Proc is an object that stores a block of code and can be executed later.

say_hello = Proc.new { puts 'Hello from Proc!' }
say_hello.call

Passing Arguments to a Proc

Procs can take arguments just like methods.

square = Proc.new { |num| num * num }
puts square.call(4)

What is a Lambda?

A lambda is similar to a Proc but has stricter argument checking.

greet = ->(name) { puts "Hello, #{name}!" }
greet.call('Alice')

Differences Between Proc and Lambda

  • Procs ignore extra arguments, while Lambdas enforce them.
  • Lambdas return control to the calling method, while Procs exit immediately.
proc_example = Proc.new { return 'Proc Exited' }
lambda_example = -> { return 'Lambda Executed' }

def test
  puts proc_example.call
  puts lambda_example.call
end

test

Congratulations!

You have learned how to use blocks, Procs, and Lambdas in Ruby.

Next, you will explore the yield keyword and passing blocks dynamically.

Blocks, Procs, and Lambdas — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Blocks, Procs, and Lambdas” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Blocks, Procs, and Lambdas”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 3 lessons in total.

What will I learn in “Blocks, Procs, and Lambdas”?

Understand how Ruby handles anonymous functions using blocks, Procs, and Lambdas. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Blocks, Procs, and Lambdas” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Methods
  2. Blocks, Procs, and Lambdas
  3. Yield and Passing Blocks
← Back to Ruby Academy