logo
  • 教程列表
  • SO官方文档
  • 普查员
    • 自定义枚举器
    • 现有方法
    • 复卷
  1. StackOverflow 文档
  2. Ruby Language 教程
  3. 普查员
  4. 现有方法

现有方法

Created: November-22, 2018

如果在没有块的情况下调用诸如 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
  • 复卷
  • 自定义枚举器

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明