这是 ruby pickaxe book 中的代码示例:
Class VowelFinder
include Enumerable
def initialize(string)
@string = string
end
def each
@string.scan(/[aeiou]/] do |vowel|
yield vowel
end
end
end
vf = VowelFinder.new("the quick brown fox jumped")
vf.inject(:+) # => 'euiooue'
我很难理解的是修改后的“每个”方法在哪里发挥作用?我假设 inject 在某个时候调用它,但不明白它为什么这样做,以及我如何在我的代码中预测或模拟这种行为。
谢谢!