るりまサーチ

最速Rubyリファレンスマニュアル検索!
198件ヒット [1-100件を表示] (0.416秒)

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

クラス

キーワード

検索結果

<< 1 2 > >>

Enumerator (52032.0)

each 以外のメソッドにも Enumerable の機能を提供するためのラッパークラスです。 また、外部イテレータとしても使えます。

...each 以外のメソッドにも Enumerable の機能を提供するためのラッパークラスです。
また、外部イテレータとしても使えます。

E
numerable モジュールは、 Module#include 先のクラスが持つ
e
ach メソッドを元に様々なメソッドを提供し...
...Array#each の繰り返しを元にして定義されます。
Enumerator
を介することにより String#each_byte のような
異なる名前のイテレータについても each と同様に Enumerable の機能を利用できます。

Enumerator
を生成するには Enumerator.newある...
...いは
Object#to_enum, Object#enum_for を利用します。また、一部の
イテレータはブロックを渡さずに呼び出すと繰り返しを実行する代わりに
enumerator
を生成して返します。

=== 注意
外部イテレータとしての機能は Fiber を用いて実装...

Enumerator::Lazy#to_enum(method = :each, *args) -> Enumerator::Lazy (35494.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...Object#to_enum と同じですが、Enumerator::Lazy を返します。

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator
を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...to_enum
素のEnumerator ではなく Enumerator::Lazy を返すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_give...
...n?
e
ach do |*val|
n.times { yield *val }
e
nd
e
lse
to_enum
(:repeat, n)
e
nd
e
nd
e
nd

r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

r = 1..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげ...

Enumerator::Lazy#to_enum(method = :each, *args) {|*args| block} -> Enumerator::Lazy (35494.0)

Object#to_enum と同じですが、Enumerator::Lazy を返します。

...Object#to_enum と同じですが、Enumerator::Lazy を返します。

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator
を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...to_enum
素のEnumerator ではなく Enumerator::Lazy を返すようになっています。

//emlist[例][ruby]{
module Enumerable
# 要素をn回ずつ繰り返すメソッド
# 例:[1,2,3].repeat(2) #=> [1,1,2,2,3,3]
def repeat(n)
raise ArgumentError if n < 0
if block_give...
...n?
e
ach do |*val|
n.times { yield *val }
e
nd
e
lse
to_enum
(:repeat, n)
e
nd
e
nd
e
nd

r = 1..10
p r.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

r = 1..Float::INFINITY
p r.lazy.map{|n| n**2}.repeat(2).first(5)
#=> [1, 1, 4, 4, 9]

# Lazy#to_enum のおかげ...

Enumerator#each(*args) -> Enumerator (35227.0)

生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。

...@param args 末尾へ追加する引数

//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"

e
num = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
e
num.each {|word| p word } # => "Yet"
# "Another"...
...# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "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 # => ["Hello", "world"]

obj = Object.new

def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
e
nd

e
num = obj.to_enum :each_a...

Enumerator#peek_values -> Array (35214.0)

Enumerator#next_values のように「次」のオブジェクトを 配列で返しますが、列挙状態を変化させません。

...
Enumerator
#next_values のように「次」のオブジェクトを
配列で返しますが、列挙状態を変化させません。

Enumerator
#next, Enumerator#next_values のように
現在までの列挙状態に応じて「次」のオブジェクトを返しますが、
next と異なり...
...eration 例外を発生します。

このメソッドは Enumerator#next_values と同様
yield

yield nil
を区別するために使えます。

//emlist[例][ruby]{
o = Object.new
def o.each
yield
yield 1
yield 1, 2
e
nd
e
= o.to_enum
p e.peek_values #=> []
e
.next
p e.peek_value...
...s #=> [1]
p e.peek_values #=> [1]
e
.next
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#next_values -> Array (35210.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]
e
nd
e
= o.to_enum
p e.next_...
...ues
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]
# yie...

Enumerator#peek -> object (35180.0)

「次」のオブジェクトを返しますが、列挙状態を変化させません。

...ません。

Enumerator
#next のように
現在までの列挙状態に応じて「次」のオブジェクトを返しますが、
next と異なり列挙状態を変更しません。

列挙が既に最後へ到達している場合は、StopIteration 例外を発生します。

//emlist[例][...
...]
e
= a.to_enum
p e.next #=> 1
p e.peek #=> 2
p e.peek #=> 2
p e.peek #=> 2
p e.next #=> 2
p e.next #=> 3
p e.next #raises StopIteration
//}

@raise StopIteration 列挙状態が既に最後へ到達しているとき
@see Enumerator#next, Enumerator#next_values, Enumerator#peek_values...

Enumerator#feed(obj) -> nil (35166.0)

Enumerator 内部の yield が返す値を設定します。

...
Enumerator
内部の yield が返す値を設定します。

これで値を設定しなかった場合は yield は nil を返します。

この値は内部で yield された時点でクリアされます。

//emlist[例][ruby]{
# (1), (2), ... (10) の順に実行される
o = Object.new
def...
...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
e
nd

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

@param obj Enumerator 内部の yield が返す値
@raise TypeError すでに値をこのメソッドでセットしている場合に発生します...

Enumerator#each -> self (35127.0)

生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。

...@param args 末尾へ追加する引数

//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"

e
num = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
e
num.each {|word| p word } # => "Yet"
# "Another"...
...# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "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 # => ["Hello", "world"]

obj = Object.new

def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
e
nd

e
num = obj.to_enum :each_a...

Enumerator#each {...} -> object (35127.0)

生成時のパラメータに従ってブロックを繰り返します。 *args を渡した場合は、生成時のパラメータ内引数末尾へ *args を追加した状態で繰り返します。 ブロック付きで呼び出された場合は、 生成時に指定したイテレータの戻り値をそのまま返します。

...@param args 末尾へ追加する引数

//emlist[例1][ruby]{
str = "Yet Another Ruby Hacker"

e
num = Enumerator.new {|y| str.scan(/\w+/) {|w| y << w }}
e
num.each {|word| p word } # => "Yet"
# "Another"...
...# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "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 # => ["Hello", "world"]

obj = Object.new

def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
e
nd

e
num = obj.to_enum :each_a...

絞り込み条件を変える

<< 1 2 > >>