Ruby - Using lambda with array each method

Introduction

The following code iterates over an array of strings, capitalizing each string in turn.

The array of capitalized strings is then assigned to the d1 variable:

Demo

d = lambda{|x| x.capitalize! }
d1 = ["hello","good day","how do you do"].each{ |s| d.call(s)}
puts(d1.inspect)        #=> ["Hello", "Good day", "How do you do"]
# from w  w w .j  ava 2 s .  c om

Result

Related Topic