Problem
upcased = ["one", "two", "three"].map {|n| puts n.upcase }
Let’s take a look at puts
as below:
>> puts "Hi"
Hi
=> nil
Note the nil
at the end: that’s the return value from puts
. After all, puts
is a method
, so it has to return something. As it happens, it always returns nil
. The printing out of the string is an action the method performs.
Similarly, evaluating the code in question we can understand that while it’s a common learner mistake to expect the result to be ["ONE", "TWO", "THREE"]
. In fact, it’s [nil, nil, nil]
. Each time through the block, the block evaluates to the return value of puts
to be nil
.