当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Enumerator.feed obj用法及代码示例


本文简要介绍ruby语言中 Enumerator.feed obj 的用法。

用法

feed obj → nil

设置 e 中的下一个 yield 返回的值。

如果未设置该值,则产量返回 nil。

该值在产生后被清除。

# Array#map passes the array's elements to "yield" and collects the
# results of "yield" as an array.
# Following example shows that "next" returns the passed elements and
# values passed to "feed" are collected as an array which can be
# obtained by StopIteration#result.
e = [1,2,3].map
p e.next           #=> 1
e.feed "a"
p e.next           #=> 2
e.feed "b"
p e.next           #=> 3
e.feed "c"
begin
  e.next
rescue StopIteration
  p $!.result      #=> ["a", "b", "c"]
end

o = Object.new
def o.each
  x = yield         # (2) blocks
  p x               # (5) => "foo"
  x = yield         # (6) blocks
  p x               # (8) => nil
  x = yield         # (9) blocks
  p x               # not reached w/o another e.next
end

e = o.to_enum
e.next              # (1)
e.feed "foo"        # (3)
e.next              # (4)
e.next              # (7)
                    # (10)

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Enumerator.feed obj。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。