當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。