0Pricing
Ruby Academy · Lesson

Blocks, Procs, and Lambdas

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

Blocks, Procs, and Lambdas is a free Ruby Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

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

2

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

3

Single-Line Blocks

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

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

4

Passing Blocks to Methods

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

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

5

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

6

Passing Arguments to a Proc

Procs can take arguments just like methods.

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

7

What is a Lambda?

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

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

8

9

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

10

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

Frequently asked questions

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

Yes — the full text of “Blocks, Procs, and Lambdas” is free to read here on the web, and the Ruby Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

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