0Pricing
Ruby Academy · Lesson

Blocks Recap

Passing blocks to methods.

Blocks Recap is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Passing Blocks to Methods

A block is a chunk of code you hand to a method. Many Ruby methods, like each, accept a block and run it for you. Blocks are the foundation of Ruby's expressive style.

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

Two Block Syntaxes

Use do ... end for multi-line blocks and curly braces { } for short one-liners. They behave the same way.

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

Block Parameters

The names between the pipes | | are the block's parameters. The method decides what to pass into them.

{ a: 1, b: 2 }.each do |key, value|
  puts "#{key} = #{value}"
end

yield Calls the Block

Inside a method, yield runs whatever block was passed. You do not declare the block in the parameter list to use yield.

def twice
  yield
  yield
end
twice { puts "Hi" }

yield with Arguments

Pass values to the block through yield. They land in the block's parameters.

def greet_each(names)
  names.each { |n| yield n }
end
greet_each(["Ann", "Bo"]) { |name| puts "Hello #{name}" }

block_given?

Calling yield with no block raises an error. Check first with block_given? to make the block optional.

def maybe
  if block_given?
    yield
  else
    puts "No block provided"
  end
end
maybe
maybe { puts "Block ran" }

Blocks Return Values

A block evaluates to its last expression. Methods like map collect those return values into a new array.

squares = [1, 2, 3, 4].map { |n| n * n }
p squares

The Value of yield

yield itself returns the block's value, so a method can transform input through a caller-supplied block.

def transform(x)
  yield x
end
result = transform(5) { |n| n + 100 }
puts result

Capturing a Block with &

Prefix the last parameter with & to capture the block as a Proc object you can store or pass on.

def run(&block)
  block.call
end
run { puts "Called via block.call" }

Numbered Parameters

Modern Ruby lets you skip naming a single block parameter using _1 (or it in Ruby 3.4+). Handy for tiny blocks.

[10, 20, 30].each { puts _1 }

Putting It Together

Blocks let methods accept behavior:

  • do...end or { } deliver a block.
  • yield runs it; block_given? tests for it.
  • &block turns it into a reusable object.
def repeat(times)
  return unless block_given?
  times.times { |i| yield i }
end
repeat(3) { |i| puts "Step #{i}" }

Quick Check

Test your understanding of blocks.

Recap

You reviewed blocks:

  • Blocks pass behavior to methods via do...end or { }.
  • yield runs the block and returns its value.
  • block_given? makes blocks optional.
  • &block captures a block as a Proc.
def wrap
  puts "<<"
  yield
  puts ">>"
end
wrap { puts "content" }

Frequently asked questions

Is the “Blocks Recap” lesson free?

Yes — the full text of “Blocks Recap” is free to read here on the web, and the Ruby Academy course includes 4 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 Recap”?

Passing blocks to methods. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Blocks Recap” 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. Blocks Recap
  2. Procs
  3. Lambdas
  4. Closures and Scope
← Back to Ruby Academy