ライブラリ
- ビルトイン (149)
キーワード
- + (6)
- feed (11)
- next (11)
-
next
_ values (11) -
peek
_ values (11) - rewind (11)
-
with
_ index (22) -
with
_ object (22)
検索結果
先頭5件
-
Enumerator
# each(*args) -> Enumerator (21266.0) -
生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。
...引数
//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"
enum = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
enum.each {|word| p word } # => "Yet"
# "Another"
# "Ruby"......# "Ruby"
# "Hacker"
//}
//emlist[例2][ruby]{
"Hello, world!".scan(/\w+/) # => ["Hello", "world"]
"Hello, world!".to_enum(:scan, /\w+/).to_a # => ["Hello", "world"]
"Hello, world!".to_enum(:scan).each(/\w+/).to_a # =>......ew
def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end
enum = obj.to_enum :each_arg, :a, :x
enum.each.to_a # => [:a, :x, []]
enum.each.equal?(enum) # => true
enum.each { |elm| elm } # => :method_returned
enum.each(:y, :z).to_... -
Enumerator
# each -> self (21166.0) -
生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。
...引数
//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"
enum = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
enum.each {|word| p word } # => "Yet"
# "Another"
# "Ruby"......# "Ruby"
# "Hacker"
//}
//emlist[例2][ruby]{
"Hello, world!".scan(/\w+/) # => ["Hello", "world"]
"Hello, world!".to_enum(:scan, /\w+/).to_a # => ["Hello", "world"]
"Hello, world!".to_enum(:scan).each(/\w+/).to_a # =>......ew
def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end
enum = obj.to_enum :each_arg, :a, :x
enum.each.to_a # => [:a, :x, []]
enum.each.equal?(enum) # => true
enum.each { |elm| elm } # => :method_returned
enum.each(:y, :z).to_... -
Enumerator
# each { . . . } -> object (21166.0) -
生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。
...引数
//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"
enum = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
enum.each {|word| p word } # => "Yet"
# "Another"
# "Ruby"......# "Ruby"
# "Hacker"
//}
//emlist[例2][ruby]{
"Hello, world!".scan(/\w+/) # => ["Hello", "world"]
"Hello, world!".to_enum(:scan, /\w+/).to_a # => ["Hello", "world"]
"Hello, world!".to_enum(:scan).each(/\w+/).to_a # =>......ew
def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end
enum = obj.to_enum :each_arg, :a, :x
enum.each.to_a # => [:a, :x, []]
enum.each.equal?(enum) # => true
enum.each { |elm| elm } # => :method_returned
enum.each(:y, :z).to_... -
Enumerator
# each(*args) { . . . } -> object (21166.0) -
生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。
...引数
//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"
enum = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
enum.each {|word| p word } # => "Yet"
# "Another"
# "Ruby"......# "Ruby"
# "Hacker"
//}
//emlist[例2][ruby]{
"Hello, world!".scan(/\w+/) # => ["Hello", "world"]
"Hello, world!".to_enum(:scan, /\w+/).to_a # => ["Hello", "world"]
"Hello, world!".to_enum(:scan).each(/\w+/).to_a # =>......ew
def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end
enum = obj.to_enum :each_arg, :a, :x
enum.each.to_a # => [:a, :x, []]
enum.each.equal?(enum) # => true
enum.each { |elm| elm } # => :method_returned
enum.each(:y, :z).to_... -
Enumerator
# next _ values -> Array (6114.0) -
「次」のオブジェクトを配列で返します。
...「次」のオブジェクトを配列で返します。
Enumerator#next とほぼ同様の挙動をします。終端まで到達した場合は
StopIteration 例外を発生させます。
このメソッドは、
yield
と
yield nil
を区別するために使えます。
next メソッ......、 IO#each_line のようにおおもとの列挙メカニズムが副作用を
伴っている場合には影響があり得ます。
//emlist[例: next と next_values の違いを][ruby]{
o = Object.new
def o.each
yield
yield 1
yield 1, 2
yield nil
yield [1, 2]
end
e = o.to_enum
p e.n......ext_values
p e.next_values
p e.next_values
p e.next_values
p e.next_values
e = o.to_enum
p e.next
p e.next
p e.next
p e.next
p e.next
## yield args next_values next
# yield [] nil
# yield 1 [1] 1
# yield 1, 2 [1, 2] [1, 2]... -
Enumerator
# peek _ values -> Array (6108.0) -
Enumerator#next_values のように「次」のオブジェクトを 配列で返しますが、列挙状態を変化させません。
...
Enumerator#next_values のように「次」のオブジェクトを
配列で返しますが、列挙状態を変化させません。
Enumerator#next, Enumerator#next_values のように
現在までの列挙状態に応じて「次」のオブジェクトを返しますが、
next と異なり......メソッドは Enumerator#next_values と同様
yield
と
yield nil
を区別するために使えます。
//emlist[例][ruby]{
o = Object.new
def o.each
yield
yield 1
yield 1, 2
end
e = o.to_enum
p e.peek_values #=> []
e.next
p e.peek_values #=> [1]
p e.peek_values #=> [1]
e.n......ext
p e.peek_values #=> [1, 2]
e.next
p e.peek_values # raises StopIteration
//}
@raise StopIteration 列挙状態が既に最後へ到達しているとき
@see Enumerator#next, Enumerator#next_values, Enumerator#peek_values... -
Enumerator
# +(enum) -> Enumerator :: Chain (3208.0) -
自身と enum 引数を続けて繰り返す Enumerator::Chain を返します。
...自身と enum 引数を続けて繰り返す Enumerator::Chain を返します。
//emlist[例][ruby]{
e = (1..3).each + [4, 5]
e.to_a #=> [1, 2, 3, 4, 5]
//}
@see Enumerable#chain... -
Enumerator
# with _ index(offset = 0) -> Enumerator (3120.0) -
生成時のパラメータに従って、要素にインデックスを添えて繰り返します。 インデックスは offset から始まります。
...バ自身です。
//emlist[例][ruby]{
str = "xyz"
enum = Enumerator.new {|y| str.each_byte {|b| y << b }}
enum.with_index {|byte, idx| p [byte, idx] }
# => [120, 0]
# [121, 1]
# [122, 2]
require "stringio"
StringIO.new("foo|bar|baz").each("|").with_index(1) {|s, i| p [s, i] }......にインデックスを添えてブロックを繰り返します。
インデックスは 0 から始まります。
Enumerator#with_index は offset 引数を受け取りますが、
each_with_index は受け取りません (引数はイテレータメソッドにそのまま渡されます)。... -
Enumerator
# with _ object(obj) -> Enumerator (3114.0) -
繰り返しの各要素に obj を添えてブロックを繰り返し、obj を返り値として返します。
...に obj を返す Enumerator を返します。
//emlist[例][ruby]{
# 0,1,2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
3.times do |x|
y << x
end
end
to_three_with_string = to_three.with_object("foo")
to_three_with_string.each do |x,string|
puts "#{string}: #{x}......"
end
# => foo:0
# => foo:1
# => foo:2
//}
@param obj 繰り返しの各要素に添えて渡されるオブジェクト
@see Enumerable#each_with_object... -
Enumerator
# next -> object (3026.0) -
「次」のオブジェクトを返します。
...し、 IO#each_line のようにおおもとの列挙メカニズムが副作用を
伴っている場合には影響があり得ます。
@raise StopIteration 列挙状態が既に最後へ到達しているとき
@see Enumerator#rewind
//emlist[例1][ruby]{
str = "xyz"
enum = str.each_byte
str.......puts enum.next
end
# => 120
# 121
# 122
//}
//emlist[例2][ruby]{
str = "xyz"
enum = str.each_byte
begin
puts enum.next while true
rescue StopIteration
puts "iteration reached at end"
end
# => 120
# 121
# 122
# iteration reached at end
puts enum.ne......xt
#=> 再度 StopIteration 例外が発生
//}
//emlist[例3: Kernel.#loop は StopIteration を捕捉します。][ruby]{
str = "xyz"
enum = str.each_byte
loop do
puts enum.next
end
# => 120
# 121
# 122
//}...