るりまサーチ

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

別のキーワード

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

ライブラリ

キーワード

検索結果

<< 1 2 3 ... > >>

Enumerable#lazy -> Enumerator::Lazy (6232.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_ma...
...flat_map {|x|
(x..z).select {|y|
x**2 + y**2 == z**2
}.map {|y|
[x, y, z]
}
}
}
end
# 最初の10個のピタゴラス数を表示する
p pythagorean_triples.take(10).force # takeはlazyなので、forceが必要です
p pythagorean_triples.first(10) # f...

Enumerable#sort_by -> Enumerator (6132.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...t[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}

Enumerable
#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
sort_by...
...//emlist[][ruby]{
p ["BAR", "FOO", "bar", "foo"].sort {|a, b| a.downcase <=> b.downcase }
//}

一方、次のように sort_by を使うと downcase の実行回数は要素数と同じです。
つまり、その部分の実行時間は O(n) のオーダーです。

//emlist[][ruby]{
p ["BAR",...
..."FOO", "bar", "foo"].sort_by {|v| v.downcase }
//}

以下の、実行回数の検証結果を参照してみてください。

//emlist[][ruby]{
class Integer
def count
$n += 1
self
end
end

ary = []
1.upto(1000) {|v| ary << rand(v) }

$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n...

Enumerable#sort_by {|item| ... } -> [object] (6132.0)

ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。

...t[例][ruby]{
class Array
def sort_by
self.map {|i| [yield(i), i] }.
sort {|a, b| a[0] <=> b[0] }.
map {|i| i[1]}
end
end
//}

Enumerable
#sort と比較して sort_by が優れている点として、
比較条件が複雑な場合の速度が挙げられます。
sort_by...
...//emlist[][ruby]{
p ["BAR", "FOO", "bar", "foo"].sort {|a, b| a.downcase <=> b.downcase }
//}

一方、次のように sort_by を使うと downcase の実行回数は要素数と同じです。
つまり、その部分の実行時間は O(n) のオーダーです。

//emlist[][ruby]{
p ["BAR",...
..."FOO", "bar", "foo"].sort_by {|v| v.downcase }
//}

以下の、実行回数の検証結果を参照してみてください。

//emlist[][ruby]{
class Integer
def count
$n += 1
self
end
end

ary = []
1.upto(1000) {|v| ary << rand(v) }

$n = 0
ary.sort {|a,b| a.count <=> b.count }
p $n...

Enumerable#max_by -> Enumerator (6120.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...です。

Enumerable
#max と Enumerable#max_by
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)
a.max_by...
...orse"]:max_by>
a.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a = %w[albatross dog horse]
a.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a.max_by(2) {|x| x.length } # => ["albatross", "horse"]
//}

//emlist[例: enum.max_by(n)は...
...Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n...

Enumerable#max_by {|item| ... } -> object | nil (6120.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...です。

Enumerable
#max と Enumerable#max_by
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)
a.max_by...
...orse"]:max_by>
a.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a = %w[albatross dog horse]
a.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a.max_by(2) {|x| x.length } # => ["albatross", "horse"]
//}

//emlist[例: enum.max_by(n)は...
...Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n...

絞り込み条件を変える

Enumerable#max_by(n) -> Enumerator (6120.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...です。

Enumerable
#max と Enumerable#max_by
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)
a.max_by...
...orse"]:max_by>
a.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a = %w[albatross dog horse]
a.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a.max_by(2) {|x| x.length } # => ["albatross", "horse"]
//}

//emlist[例: enum.max_by(n)は...
...Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n...

Enumerable#max_by(n) {|item| ... } -> Array (6120.0)

各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。

...です。

Enumerable
#max と Enumerable#max_by
違いは Enumerable#sort と Enumerable#sort_by の違いと同じです。

ブロックを省略した場合は Enumerator を返します。

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)
a.max_by...
...orse"]:max_by>
a.max_by { |x| x.length } # => "albatross"
//}

//emlist[例][ruby]{
a = %w[albatross dog horse]
a.max_by(2) # => #<Enumerator: ["albatross", "dog", "horse"]:max_by(2)>
a.max_by(2) {|x| x.length } # => ["albatross", "horse"]
//}

//emlist[例: enum.max_by(n)は...
...Enumerable#wsampleを使用します。][ruby]{
module Enumerable
# weighted random sampling.
#
# Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n...

Enumerable#any? -> bool (6108.0)

すべての要素が偽である場合に false を返します。 真である要素があれば、ただちに true を返します。

...[ruby]{
require 'set'
p Set[1, 2, 3].any? {|v| v > 3 } # => false
p Set[1, 2, 3].any? {|v| v > 1 } # => true
p Set[].any? {|v| v > 0 } # => false
p Set[nil, true, 99].any? # => true
p Set[].any? # => false
//}

@see Array#any?...
...す。

//emlist[例][ruby]{
require 'set'
p Set[1, 2, 3].any? {|v| v > 3 } # => false
p Set[1, 2, 3].any? {|v| v > 1 } # => true
p Set[].any? {|v| v > 0 } # => false
p Set['ant', 'bear', 'cat'].any?(/d/) # => false
p Set[nil, true, 99].any?(Integer) # => true...
...p Set[nil, true, 99].any? # => true
p Set[].any? # => false
//}

@see Array#any?...

Enumerable#any? {|item| ... } -> bool (6108.0)

すべての要素が偽である場合に false を返します。 真である要素があれば、ただちに true を返します。

...す。

//emlist[例][ruby]{
require 'set'
p Set[1, 2, 3].any? {|v| v > 3 } # => false
p Set[1, 2, 3].any? {|v| v > 1 } # => true
p Set[].any? {|v| v > 0 } # => false
p Set['ant', 'bear', 'cat'].any?(/d/) # => false
p Set[nil, true, 99].any?(Integer) # => true...
...p Set[nil, true, 99].any? # => true
p Set[].any? # => false
//}

@see Array#any?...
<< 1 2 3 ... > >>