带有自定义方法的符号#to_proc

我喜欢在Ruby中如何使用Symbol#to_proc将方法作为块传递:

 [1.0, 2.0, 3.0].map(&:to_i) #=> [1, 2, 3] 

我也可以定义自己的lambda, times_two ,并将其作为块传递:

 times_two = ->(x) {x * 2} [1, 2, 3].map(&times_two) #=> [2, 4, 6] 

虽然我似乎无法通过times_two作为符号:

 [1, 2, 3].map(&:times_two) #=> ArgumentError: wrong number of arguments (0 for 1) 

但是,当我尝试使用方法执行相同操作时,我收到错误:

 def times_three(x) x * 3 end [1, 2, 3].map(&times_three) #=> ArgumentError: wrong number of arguments (0 for 1) [1, 2, 3].map(&:times_three) #=> ArgumentError: wrong number of arguments (0 for 1) 

我猜我不能这样做因为times_three是一种方法,而不是Proc。

那么如何定义自定义方法,以便它们可以像to_i第一个示例中的to_i一样以Symbol#to_proc方式使用?

例如,我该怎么做?

 [1, 2, 3].map(&:times_three) #=> [3, 6, 9] 

编辑:我观看了下面发布的video,显然你可以使用method方法接近Symbol#to_proc:

 def times_three(x) x * 3 end t_three = method(:times_three) [1, 2, 3].map(&t_three) #=> [3, 6, 9] 

但是,它不是符号#to_proc:

 [1, 2, 3].map(&:t_three) #=> NoMethodError: undefined method `t_three' for 1:FixNum 

 class Integer def times_three return self * 3 end end 

现在,因为times_three现在是Integer类的一个方法,你可以做符号来处理…

 [1, 2, 3].map(&:times_three) 

如果要访问不属于对象类但作用于对象的方法,则需要将该对象作为参数传递给方法…

 def times_three(x) x * 3 end [1, 2, 3].map{|i| times_three(i) } 

symbol to procsymbol to proc需要将对象用作接收器。

 [1, 2, 3].map(&:some_action) 

相当于

 [1, 2, 3].map{|i| i.some_action} 

您必须在IntegerNumeric上定义times_three

Peter Cooper解释的符号到Proc: https : //www.youtube.com/watch?v = aISNtCAZlMg