Ruby Blocks & Closures

Topics 

  • Blocks 
    • What is a block? 
    • How does a block look like? 
    • How does a block get passed and executed? 
  • Proc 
  • & (Ampersand) 
  • Lambda 
  • Closure

What is a block (code block)? 

  • Block is basically a chunk of code
    • You can think of it as a nameless function as well 
  • You can pass a block to “another function” as an argument (I will call that “another function” a “target function” or “target method” in this presentation), and then that target function can execute the passed-in code block 
    • For example, a target function could perform iteration by passing one item at a time to the block

 

Download course content