Blocks Within Blocks
You’ve already seen how to use a block to iterate over an array. In the next example (also in 4blocks.rb), I use one block to iterate over an array of strings, assigning each string in turn to the block variable s
. A second block is then passed to the caps
method in order to capitalize the string:
["hello","good day","how do you do"].each{ |s| caps( s ){ |x| x.capitalize! puts( x ) } }
This results in the following output:
Hello Good day How do you do
Get The Book of Ruby now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.