るりまサーチ

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

別のキーワード

  1. enumerable max
  2. enumerable min
  3. enumerable max_by
  4. enumerable min_by
  5. enumerable count

ライブラリ

クラス

モジュール

検索結果

<< 1 2 3 ... > >>

Enumerable#lazy -> Enumerator::Lazy (39248.0)

自身を lazy な Enumerator に変換したものを返します。

...自身を lazy な Enumerator に変換したものを返します。

この Enumerator は、以下のメソッドが遅延評価を行う (つまり、配列ではな
くEnumeratorを返す) ように再定義されています。

* map/collect
* flat_map/collect_concat
* select/find_all
*...
...のみlazy)
* cycle (※一貫性のため、ブロックを渡さないケースのみlazy)

以下はピタゴラス数 (a**2 + b**2 = c**2 を満たす自然数 a, b, c の組) を
列挙するプログラムです。

//emlist[例][ruby]{
def pythagorean_triples
(1..Float::INFINITY).lazy.flat...
...数を表示する
p pythagorean_triples.take(10).force # takeはlazyなので、forceが必要です
p pythagorean_triples.first(10) # firstはeagerです
# 100より小さいピタゴラス数を表示する
p pythagorean_triples.take_while { |*, z| z < 100 }.force
//}

@see Enumerator::Lazy...

Enumerable#sum(init=0) -> object (21012.0)

要素の合計を返します。

...=> 20 }.sum([]) #=> [1, 10, 2, 20]
"a\nb\nc".each_line.lazy.map(&:chomp).sum("") #=> "abc"
[[1], [[2]], [3]].sum([]) #=> [1, [2], 3]
//}

"+" メソッドが再定義されている場合、Enumerable#sum は再定義を無視することがあります(例えばInteger#+...

Enumerable#sum(init=0) {|e| expr } -> object (21012.0)

要素の合計を返します。

...=> 20 }.sum([]) #=> [1, 10, 2, 20]
"a\nb\nc".each_line.lazy.map(&:chomp).sum("") #=> "abc"
[[1], [[2]], [3]].sum([]) #=> [1, [2], 3]
//}

"+" メソッドが再定義されている場合、Enumerable#sum は再定義を無視することがあります(例えばInteger#+...

Enumerator::Lazy (6060.0)

map や select などのメソッドの遅延評価版を提供するためのクラス。

...換性のため、ブロックを渡さないケースのみlazy)

Lazy
オブジェクトは、Enumerable#lazyメソッドによって生成されます。

Lazy
から値を取り出すには、Enumerator::Lazy#force または
Enumerable
#first を呼びます。

//emlist[例][ruby]{
# 二乗して...
...偶数になるような整数を、小さい方から5個表示する
p 1.step.lazy.select{|n| (n**2).even?}.first(5)
# LTSV (http://ltsv.org/) 形式のログファイルから検索を行う
# Enumerator::Lazy#map は配列ではなく Enumerator を返すため、
# 巨大な配列を確保し...
...ようとしてメモリを使い切ったりはしない
open("log.txt"){|f|
f.each_line.lazy.map{|line|
Hash[line.split(/\t/).map{|s| s.split(/:/, 2)}]
}.select{|hash|
hash["req"] =~ /GET/ && hash["status"] == "200"
}.each{|hash|
p hash
}
}
//}...

Enumerator::Lazy#grep(pattern) {|item| ... } -> Enumerator::Lazy (3181.0)

Enumerable#grep と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#grep と同じですが、配列ではなくEnumerator::Lazy を返します。

//emlist[例][ruby]{
(100..Float::INFINITY).lazy.map(&:to_s).grep(/\A(\d)\1+\z/)
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 100..Infinity>:map>:grep(/\A(\d)\1+\z/)>
(100..Float::INFINIT...
...Y).lazy.map(&:to_s).grep(/\A(\d)\1+\z/).take(10).force
# => ["111", "222", "333", "444", "555", "666", "777", "888", "999", "1111"]
//}

@see Enumerable#grep, Enumerable#grep_v, Enumerator::Lazy#grep_v...

絞り込み条件を変える

Enumerator::Lazy#grep_v(pattern) {|item| ... } -> Enumerator::Lazy (3181.0)

Enumerable#grep_v と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#grep_v と同じですが、配列ではなくEnumerator::Lazy を返します。

//emlist[例][ruby]{
(100..Float::INFINITY).lazy.map(&:to_s).grep_v(/(\d).*\1/)
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 100..Infinity>:map>:grep_v(/(\d).*\1/)>
(100..Float::INFINIT...
...Y).lazy.map(&:to_s).grep_v(/(\d).*\1/).take(15).force
# => ["102", "103", "104", "105", "106", "107", "108", "109", "120", "123", "124", "125", "126", "127", "128"]
//}

@see Enumerable#grep_v, Enumerable#grep, Enumerator::Lazy#grep...

Enumerator::Lazy#take_while -> Enumerator::Lazy (3170.0)

Enumerable#take_while と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#take_while と同じですが、配列ではなくEnumerator::Lazy を返します。

//emlist[例][ruby]{
1.step.lazy.zip(('a'..'z').cycle).take_while { |e| e.first < 100_000 }
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: 1:step>>:zip(#<Enumerator: "a"...
....."z":cycle>)>:take_while>

1.step.lazy.zip(('a'..'z').cycle).take_while { |e| e.first < 100_000 }.force.last(5)
# => [[99995, "y"], [99996, "z"], [99997, "a"], [99998, "b"], [99999, "c"]]
//}

@see Enumerable#take_while...

Enumerator::Lazy#take_while {|item| ... } -> Enumerator::Lazy (3170.0)

Enumerable#take_while と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#take_while と同じですが、配列ではなくEnumerator::Lazy を返します。

//emlist[例][ruby]{
1.step.lazy.zip(('a'..'z').cycle).take_while { |e| e.first < 100_000 }
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: 1:step>>:zip(#<Enumerator: "a"...
....."z":cycle>)>:take_while>

1.step.lazy.zip(('a'..'z').cycle).take_while { |e| e.first < 100_000 }.force.last(5)
# => [[99995, "y"], [99996, "z"], [99997, "a"], [99998, "b"], [99999, "c"]]
//}

@see Enumerable#take_while...

Enumerator::Lazy#grep(pattern) {|item| ... } -> Enumerator::Lazy (3169.0)

Enumerable#grep と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#grep と同じですが、配列ではなくEnumerator::Lazy を返します。

//emlist[例][ruby]{
(100..Float::INFINITY).lazy.map(&:to_s).grep(/\A(\d)\1+\z/)
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 100..Infinity>:map>:grep(/\A(\d)\1+\z/)>
(100..Float::INFINIT...
...Y).lazy.map(&:to_s).grep(/\A(\d)\1+\z/).take(10).force
# => ["111", "222", "333", "444", "555", "666", "777", "888", "999", "1111"]
//}

@see Enumerable#grep...

Enumerator::Lazy#take(n) -> Enumerator::Lazy (3169.0)

Enumerable#take と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#take と同じですが、配列ではなくEnumerator::Lazy を返します。

n が大きな数 (100000とか) の場合に備えて再定義されています。
配列が必要な場合は Enumerable#first を使って下さい。

@param n 要素数を指定します。

@raise Arg...
...umentError n に負の数を指定した場合に発生します。

//emlist[例][ruby]{
1.step.lazy.take(5)
# => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: 1:step>>:take(5)>

1.step.lazy.take(5).force
# => [1, 2, 3, 4, 5]
//}

@see Enumerable#take...

絞り込み条件を変える

Enumerator::Lazy#zip(*lists) -> Enumerator::Lazy (3169.0)

Enumerable#zip と同じですが、配列ではなくEnumerator::Lazy を返します。

...
Enumerable
#zip と同じですが、配列ではなくEnumerator::Lazy を返します。

ただし一貫性のため、ブロック付きで呼び出した場合は Enumerable#zip と
同じ挙動になります。

//emlist[例][ruby]{
1.step.lazy.zip(('a'..'z').cycle)
# => #<Enumerator::Lazy: #...
...<Enumerator::Lazy: #<Enumerator: 1:step>>:zip(#<Enumerator: "a".."z":cycle>)>

1.step.lazy.zip(('a'..'z').cycle).take(30).force.last(6)
# => [[25, "y"], [26, "z"], [27, "a"], [28, "b"], [29, "c"], [30, "d"]]
//}

@see Enumerable#zip...
<< 1 2 3 ... > >>