Enumerator class的文档页面上列出了以下方法:

  • ::new
  • #each
  • #each_with_index
  • #each_with_object
  • #feed
  • #inspect
  • #next
  • #next_values
  • #peek
  • #peek_values
  • #rewind
  • #size
  • #with_index
  • #with_object

  • 但是,当我在irb中执行以下操作时,我会收到false:
    >> Enumerator.methods.include?(:next)
    false
    

    这是因为即使该方法可以在此类的实例上使用,但该方法并未在类本身上明确定义,即它是继承的?考虑到这一点,我还检查了Enumerator super class ,发现它们也不包含此方法:
    >> Enumerator.superclass.methods.include?(:next)
    false
    >> Enumerator.superclass.superclass.methods.include?(:next)
    false
    

    我确定我会忽略一些基本的东西。

    最佳答案

    #next是Enumerator类的实例方法。使用Enumerator.instance_methods.include?(:next)将返回true

    关于ruby - 为什么 `next`没有作为 `Enumerator`类的方法列出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59591323/

    10-12 01:01