るりまサーチ

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

別のキーワード

  1. psych psych_y
  2. kernel y
  3. psych y
  4. kernel psych_y
  5. y kernel

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 > >>

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

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

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

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...継がれるように、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 ArgumentErr...
...n.times { yield *val }
end
else
to_enum
(:repeat, n)
end
end
end

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 のおかげで、repeat の返り値...

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

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

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

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...継がれるように、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 ArgumentErr...
...n.times { yield *val }
end
else
to_enum
(:repeat, n)
end
end
end

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 のおかげで、repeat の返り値...

Enumerator#peek_values -> Array (6237.0)

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

...StopIteration 例外を発生します。

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

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

//emlist[例][ruby]{
o = Object.new
def o.each
y
ield
y
ield 1
y
ield 1, 2
end
e = o.to_enum
p
e.peek_values #=> []
e.next
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...

Enumerable#compact -> Array (6207.0)

self から nil を取り除いた配列を生成して返します。

...self から nil を取り除いた配列を生成して返します。

//emlist[][ruby]{
def with_nils
y
ield 1
y
ield 2
y
ield nil
y
ield 3
end

to_enum
(:with_nils).compact # => [1, 2, 3]
//}

@see Array#compact...

Enumerator::Lazy#enum_for(method = :each, *args) -> Enumerator::Lazy (3155.0)

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

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

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...継がれるように、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 ArgumentErr...
...n.times { yield *val }
end
else
to_enum
(:repeat, n)
end
end
end

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 のおかげで、repeat の返り値...

絞り込み条件を変える

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

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

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

to_enum
は「ブロック付きで呼ぶとループを実行し、ブロックを省略した場合は
Enumerator を返す」ようなメソッドを定義するときによく使われます。
このときに lazy 性が...
...継がれるように、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 ArgumentErr...
...n.times { yield *val }
end
else
to_enum
(:repeat, n)
end
end
end

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 のおかげで、repeat の返り値...

Enumerator#next_values -> Array (173.0)

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

...返します。

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

このメソッドは、
y
ield

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

next メソッドによる外部列挙の状態は他の...
...[ruby]{
o = Object.new
def o.each
y
ield
y
ield 1
y
ield 1, 2
y
ield nil
y
ield [1, 2]
end
e = o.to_enum
p
e.next_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...
...# yield 1 [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#p...

Enumerator#each -> self (67.0)

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

...タの戻り値をそのまま返します。

@param 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"
# "An...
...Ruby"
# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "Ruby"
# "Hacker"
//}

//emlist[例2][ruby]{...
...orld"]
"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)
y
ield a
y
ield b
y
ield rest
:method_returned
end

enum = obj.to_enum :each_arg, :a, :x

en...

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

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

...タの戻り値をそのまま返します。

@param 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"
# "An...
...Ruby"
# "Hacker"

str.scan(/\w+/) {|word| p word } # => "Yet"
# "Another"
# "Ruby"
# "Hacker"
//}

//emlist[例2][ruby]{...
...orld"]
"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)
y
ield a
y
ield b
y
ield rest
:method_returned
end

enum = obj.to_enum :each_arg, :a, :x

en...
<< 1 2 > >>