how does Array#map have parameter to do something like this?

后端 未结 2 1337
梦毁少年i
梦毁少年i 2020-12-04 00:36
def double(a)
 a*2
end

method_object = method(:double)

and here\'s my question, how does this code:

[1,3,5,6].map(&method_obje         


        
2条回答
  •  青春惊慌失措
    2020-12-04 01:15

    you can extend class integer:

    class Integer
      def double
        self*2
      end
    end
    
    [1,2,3,4].map(&:double)
    [
        [0] 2,
        [1] 4,
        [2] 6,
        [3] 8
    ]
    

提交回复
热议问题