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
endTwo 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
- Blocks Recap
- Procs
- Lambdas
- Closures and Scope