类 Method

Method 对象由 Object#method 创建,并与特定的对象(而不仅仅是类)关联。它们可以用于在对象内调用方法,并作为与迭代器关联的代码块。它们也可以从一个对象解绑(创建一个 UnboundMethod)并绑定到另一个对象。

class Thing
  def square(n)
    n*n
  end
end
thing = Thing.new
meth  = thing.method(:square)

meth.call(9)                 #=> 81
[ 1, 2, 3 ].collect(&meth)   #=> [1, 4, 9]

[ 1, 2, 3 ].each(&method(:puts)) #=> prints 1, 2, 3

require 'date'
%w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
#=> [#<Date: 2017-03-01 ((2457814j,0s,0n),+0s,2299161j)>, #<Date: 2017-03-02 ((2457815j,0s,0n),+0s,2299161j)>]