Free Map Lesson

By on

Here we have a method called acronify, we are going to use the Enumerable Method #map, let’s find out what it does.

def acronify(sentence) { |word| word[0].upcase }.join("")

puts acronify(["free", "map", "lesson"])
# => FML

That’s right, acronify, makes an acronym out of the given array! Let’s try another example.

puts acronify(["fund", "my", "learning"])
# => FML

What the #map method does is it goes through each element in the array and executes the code inside the curly braces. So for every word in the array, it takes the first character (index of 0) and turns it into uppercase, it does this until the end of the array, using .join(""), the output is now a string.

["want", "to", "feedback", "?"]