0Pricing
Ruby Academy · Lesson

Blocks Recap

Passing blocks to methods.

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 }

All lessons in this course

  1. Blocks Recap
  2. Procs
  3. Lambdas
  4. Closures and Scope
← Back to Ruby Academy