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

后端 未结 7 1237
面向向阳花
面向向阳花 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:44

    I'm not sure about the Symbol#with already posted, I simplified it quite a bit and it works well:

    class Symbol
      def with(*args, &block)
        lambda { |object| object.public_send(self, *args, &block) }
      end
    end
    

    (also uses public_send instead of send to prevent calling private methods, also caller is already used by ruby so this was confusing)

提交回复
热议问题