Blocks , lambda, procs and closure Table of content 1. Blocks 2. Lambda 3. Procs 4. Closure Blocks Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {}
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments| block body } Syntax of block using do-end ['List of items'].each do | block arguments | # block body end Example of block declared as do-end with each method. [ 1 , 2 , 3 ].each do |num| puts num end Output $ ruby block_with_each.rb 1 2 3 $ Blocks can also be saved in variables or passed as argument to another function. yield is a Ruby keyword that is used to call a block. When you use the yield ...
Software Engineering Crunch (New post every week)