In many cases developers will use the map method with the block for doing simple operations, instead, they can use “& operator” for better code.
Don’ts
vowels = ['a','b','c'] new_vowels = vowels.map(&:upcase) puts new_vowels -> [‘A’, ‘E’, ‘I’, ‘O’, ‘U’]
Do’s
vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] new_vowels = vowels.map { |vowel| vowel.upcase } puts new_vowels -> [‘A’, ‘E’, ‘I’, ‘O’, ‘U’]
Leave a Reply