Can you supply arguments to the map(&:method) syntax in Ruby?

后端 未结 7 1195
面向向阳花
面向向阳花 2020-11-22 16:59

You\'re probably familiar with the following Ruby shorthand (a is an array):

a.map(&:method)

For example, try the followin

7条回答
  •  无人及你
    2020-11-22 17:56

    Instead of patching core classes yourself, as in the accepted answer, it's shorter and cleaner to use the functionality of the Facets gem:

    require 'facets'
    a = [1,3,5,7,9]
    a.map &:+.(2)
    

提交回复
热议问题