现有方法

如果在没有块的情况下调用诸如 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