enumerator.so

请参考Enumerable::Enumerator

若使用require语句来包含该模块时,Object类将新增下列方法

Object#to_enum(method = :each, *args)
Object#enum_for(method = :each, *args)

返回Enumerable::Enumerator.new(self, method, *args)

例:
    str = "xyz"

    enum = str.enum_for(:each_byte)
    a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"]

    # protects an array from being modified
    a = [1, 2, 3]
    some_method(a.to_enum)

若使用require语句来包含该模块时,Enumerable模块将新增下列方法

Enumerable#each_slice(n) {...}

依次将n个元素传给块来进行迭代操作。

例:
    (1..10).each_slice(3) {|a| p a}
    # outputs below
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10]
Enumerable#enum_slice(n)

返回Enumerable::Enumerator.new(self, :each_slice, n)

Enumerable#each_cons(n) {...}

将由连续的n个元素构成的数组传给块进行迭代操作。

例:
    (1..10).each_cons(3) {|a| p a}
    # outputs below
    [1, 2, 3]
    [2, 3, 4]
    [3, 4, 5]
    [4, 5, 6]
    [5, 6, 7]
    [6, 7, 8]
    [7, 8, 9]
    [8, 9, 10]
Enumerable#enum_cons(n)

返回Enumerable::Enumerator.new(self, :each_cons, n)

Enumerable#enum_with_index

返回Returns Enumerable::Enumerator.new(self, :each_with_index)