現有方法

如果在沒有塊的情況下呼叫諸如 each 之類的迭代方法,則應返回 Enumerator 。

這可以使用 enum_for 方法完成:

def each
  return enum_for :each unless block_given?

  yield :x
  yield :y
  yield :z
end

這使程式設計師能夠組成 Enumerable 操作:

each.drop(2).map(&:upcase).first
# => :Z