るりまサーチ

最速Rubyリファレンスマニュアル検索!
20件ヒット [1-20件を表示] (0.018秒)
トップページ > クラス:Enumerator[x] > クエリ:new[x]

別のキーワード

  1. openssl new
  2. rexml new
  3. new openssl
  4. _builtin new

関連するキーワード

  1. openssl
  2. rexml
  3. openssl
  4. _builtin
  5. resolv

ライブラリ

キーワード

検索結果

Enumerator.new {|y| ... } -> Enumerator (18122)

Enumerator オブジェクトを生成して返します。与えられたブロックは Enumerator::Yielder オブジェクトを 引数として実行されます。

...
Enumerator
オブジェクトを生成して返します。与えられたブロックは Enumerator::Yielder オブジェクトを
引数として実行されます。

生成された Enumerator オブジェクトに対して each を呼ぶと、この生成時に指定されたブロックを...
...れたブロックが繰り返されます。

new
に渡されたブロックが終了した時点で each の繰り返しが終わります。
このときのブロックの返り値が each の返り値となります。

例:

enum = Enumerator.new{|y|
(1..10).each{|i|
y << i if i...
...% 5 == 0
}
}
enum.each{|i| p i }

#=> 5
10


fib = Enumerator.new { |y|
a = b = 1
loop {
y << a
a, b = b, a + b
}
}

p fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]...

Enumerator.new(obj, method = :each, *args) -> Enumerator (18112)

オブジェクト obj について、 each の代わりに method という 名前のメソッドを使って繰り返すオブジェクトを生成して返します。 args を指定すると、 method の呼び出し時に渡されます。

...
@param method イテレータメソッドの名前を表すシンボルまたは文字列
@param args イテレータメソッドの呼び出しに渡す任意個の引数

例:

str = "xyz"

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

Enumerator#with_index(offset = 0) -> Enumerator (13)

生成時のパラメータに従って、要素にインデックスを添えて繰り返します。 インデックスは offset から始まります。

...自身です。

例:
str = "xyz"

enum = Enumerator.new(str, :each_byte)
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_index(offset = 0) {|(*args), idx| ... } -> object (13)

生成時のパラメータに従って、要素にインデックスを添えて繰り返します。 インデックスは offset から始まります。

...自身です。

例:
str = "xyz"

enum = Enumerator.new(str, :each_byte)
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#each {...} -> object (7)

生成時のパラメータに従ってブロックを繰り返します。 生成時に指定したイテレータの戻り値をそのまま返します。

...ます。
生成時に指定したイテレータの戻り値をそのまま返します。

例:
str = "Yet Another Ruby Hacker"

enum = Enumerator.new(str, :scan, /\w+/)
enum.each {|word| p word } # => "Yet"
# "Another"...

絞り込み条件を変える

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

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

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

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

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

# 例
# (1), (2), ... (10) の順に実行される
o = Object.new
def o.each...
...o_enum
e.next # (1)
e.feed "foo" # (3)
e.next # (4)
e.next # (7)
# (10)
@param obj Enumerator 内部の yield が返す値
@raise TypeError すでに値をこのメソッドでセットしている場合に発生します...

Enumerator#next_values -> Array (7)

「次」のオブジェクトを配列で返します。

...「次」のオブジェクトを配列で返します。

Enumerator
#next とほぼ同様の挙動をします。終端まで到達した場合は
StopIteration 例外を発生させます。

このメソッドは、
yield

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

next メソッ...
...挙メカニズムが副作用を
伴っている場合には影響があり得ます。

# 例、 next と next_values の違いを
o = Object.new
def o.each
yield
yield 1
yield 1, 2
yield nil
yield [1, 2]
end
e = o.to_enum
p e.next_values
p e.next_values
p e.nex...
...[1] 1
# yield 1, 2 [1, 2] [1, 2]
# yield nil [nil] nil
# yield [1, 2] [[1, 2]] [1, 2]

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

Enumerator#peek_values -> Array (7)

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

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

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

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

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

# 例
o = Object.new
def o.each
yield
yield 1
yield 1, 2
end
e = o.to_enum
p e.peek_val...
...p e.peek_values #=> [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#with_object(obj) -> Enumerator (7)

繰り返しの各要素に obj を添えてブロックを繰り返し、obj を返り値として返します。

...なかった場合は、上で説明した繰り返しを実行し、
最後に obj を返す Enumerator を返します。

==== 例
# 0,1,2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
3.times do |x|
y << x
end
end

to_three_with_string = to_three.wi...

Enumerator#with_object(obj) {|(*args), memo_obj| ... } -> object (7)

繰り返しの各要素に obj を添えてブロックを繰り返し、obj を返り値として返します。

...なかった場合は、上で説明した繰り返しを実行し、
最後に obj を返す Enumerator を返します。

==== 例
# 0,1,2 と呼びだす enumeratorを作る
to_three = Enumerator.new do |y|
3.times do |x|
y << x
end
end

to_three_with_string = to_three.wi...

絞り込み条件を変える