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 )# from   w ww. j a  v  a  2 s  . co m
end

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

Result

Related Topic