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

  • 關於本站
  • 免責聲明