Using & To Create Procs

by

in

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’]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *