Intern Track Roadmap

Ruby blocks (do..end)

This course explains how Ruby methods receive and run blocks. It starts with yield, which runs the block passed to a method, and shows how yield hands values in and returns the block's result. It covers block_given? for optional blocks and the LocalJumpError raised when no block is given. It shows how &block captures a block as a Proc and forwards it to another method. The lessons rebuild each, map and select, take a brief look at Proc and lambda, and end with a small challenge.

What you'll learn

  • ✓How yield runs the block a method received
  • ✓Passing values to a block with yield and using its return value
  • ✓Making a block optional with block_given?
  • ✓Capturing a block with &block and forwarding it to another method
  • ✓Rebuilding each, map and select with a loop, yield and an accumulator
  • ✓Recognizing Proc and lambda and how they differ on argument count
Free preview · Lesson 1 2 min read

yield: running the block a method received

The last three courses used each, map and select constantly, methods that receive a block and run it. None of them showed how a method does that. The answer is yield.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...