Ruby - Blocks Within Blocks

Introduction

The following code uses nested blocks.

The first block iterates over an array of strings, assigning each string in turn to the block variable s.

A second block is passed to the caps method in order to capitalize the string:

Demo

def caps( arg )
    yield( arg )# w w w  .  jav  a 2s  .c  om
end

["hello","good day","how do you do"].each{
    |s|
    caps( s ){ |x| x.capitalize!
        puts( x )
    }
}

Result

Related Topic