← Back to Ruby Course | Chapter 8: Blocks, Procs & Lambdas | Lesson 1 of 6

Block Basics (Review)

In this page:

  1. Block Basics (Review)

Block Basics (Review)

A block is the code attached to a method call with { } or do...end, and it isn't an object by itself -- it only exists as part of that specific call, invoked via yield. This tutorial revisits blocks specifically to contrast them with Proc and Lambda, which turn similar logic into a reusable, storable object. Understanding this distinction is key to picking the right tool for a given situation.

Note: A bare block can't be stored in a variable or passed around on its own -- for that, you need a Proc or Lambda.

Example: Block Basics (Review)

markup
def run_twice
  yield
  yield
end

run_twice { puts "Running from a block" }
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.