るりまサーチ

最速Rubyリファレンスマニュアル検索!
223件ヒット [1-100件を表示] (0.068秒)
トップページ > クエリ:-[x] > クエリ:Ruby[x] > クエリ:end[x] > モジュール:Enumerable[x]

別のキーワード

  1. _builtin -
  2. open-uri open
  3. irb/input-method new
  4. irb/input-method gets
  5. matrix -

ライブラリ

キーワード

検索結果

<< 1 2 3 > >>

Enumerable#chunk {|elt| ... } -> Enumerator (209.0)

要素を前から順にブロックで評価し、その結果によって 要素をチャンクに分けた(グループ化した)要素を持つ Enumerator を返します。

...//emlist[][ruby]{
enum.chunk {|elt| key }.each {|key, ary| do_something }
//}

例として、整数列を連続する奇数/偶数に分ける例を見てみます。
「n.even?」の値が切り替わるところで区切られているのがわかるでしょう。

//emlist[例][ruby]{
[3, 1, 4...
...文字の違いを無視するため upcase しています。

//emlist[例][ruby]{
# ファイルのエンコーディングは実際のファイルに合わせてください。
open("/usr/share/dict/words", "r:iso-8859-1") {|f|
f.chunk {|line| line[0].upcase }.each {|ch, lines| p [ch, lines.le...
...釈されます。

それ以外のアンダースコアで始まるシンボルを指定した場合は例外が発生します。

//emlist[例][ruby]{
[1, 2].chunk { |item| :_underscore }.to_a
# => RuntimeError: symbols beginning with an underscore are reserved

# 「.to_a」無しだと Enumerat...

Enumerable#sort_by -> Enumerator (155.0)

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

...つまり、以下とほぼ同じ動作をします。

//emlist[例][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 が優れている点として、
比較条件...
...//emlist[][ruby]{
p ["BAR", "FOO", "bar", "foo"].sort {|a, b| a.downcase <=> b.downcase }
//}

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

//emlist[][ruby]{
p ["BAR",...
...さい。

//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 # => 18200

$n = 0
ary.sort_by {|v| v.count }
p $n # => 1000
//}

Enumerable
#sort_by は安定では...

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

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

...つまり、以下とほぼ同じ動作をします。

//emlist[例][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 が優れている点として、
比較条件...
...//emlist[][ruby]{
p ["BAR", "FOO", "bar", "foo"].sort {|a, b| a.downcase <=> b.downcase }
//}

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

//emlist[][ruby]{
p ["BAR",...
...さい。

//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 # => 18200

$n = 0
ary.sort_by {|v| v.count }
p $n # => 1000
//}

Enumerable
#sort_by は安定では...

Enumerable#max_by -> Enumerator (137.0)

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

...返すかは不定です。

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

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

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)...
...][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) {|v| rand ** (1.0/yield(v)) }
end

end
...
...e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 20000
h = a.group_by {|x| x }
-
10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
#=> *
# ***
# ******
# ***********
# ******************...

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

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

...返すかは不定です。

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

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

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)...
...][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) {|v| rand ** (1.0/yield(v)) }
end

end
...
...e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 20000
h = a.group_by {|x| x }
-
10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
#=> *
# ***
# ******
# ***********
# ******************...

絞り込み条件を変える

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

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

...返すかは不定です。

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

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

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)...
...][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) {|v| rand ** (1.0/yield(v)) }
end

end
...
...e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 20000
h = a.group_by {|x| x }
-
10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
#=> *
# ***
# ******
# ***********
# ******************...

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

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

...返すかは不定です。

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

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

@param n 取得する要素数。

//emlist[例][ruby]{
a = %w(albatross dog horse)...
...][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) {|v| rand ** (1.0/yield(v)) }
end

end
...
...e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 20000
h = a.group_by {|x| x }
-
10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
#=> *
# ***
# ******
# ***********
# ******************...

Enumerable#each_with_index(*args) -> Enumerator (125.0)

要素とそのインデックスをブロックに渡して繰り返します。

...されます。

//emlist[例][ruby]{
[5, 10, 15].each_with_index do |n, idx|
p [n, idx]
end

# => [5, 0]
# [10, 1]
# [15, 2]
//}

//emlist[引数ありの例][ruby]{
require 'stringio'
StringIO.new("foo|bar|baz").each_with_index("|") do |s, i|
p [s, i]
end

# => ["foo|", 0]
# ["bar|", 1]...

Enumerable#each_with_index(*args) {|item, index| ... } -> self (125.0)

要素とそのインデックスをブロックに渡して繰り返します。

...されます。

//emlist[例][ruby]{
[5, 10, 15].each_with_index do |n, idx|
p [n, idx]
end

# => [5, 0]
# [10, 1]
# [15, 2]
//}

//emlist[引数ありの例][ruby]{
require 'stringio'
StringIO.new("foo|bar|baz").each_with_index("|") do |s, i|
p [s, i]
end

# => ["foo|", 0]
# ["bar|", 1]...

Enumerable#max {|a, b| ... } -> object | nil (120.0)

ブロックの評価結果で各要素の大小判定を行い、最大の要素、もしくは最大の n 要素が入った降順の配列を返します。 引数を指定しない形式では要素が存在しなければ nil を返します。 引数を指定する形式では、空の配列を返します。

...eError ブロックが整数以外を返したときに発生します。

//emlist[例][ruby]{
class Person
attr_reader :name, :age

def initialize(name, age)
@name = name
@age = age
end

end


people = [
Person.new("sato", 55),
Person.new("sato", 33),
Person.new("sato", 11),...

絞り込み条件を変える

<< 1 2 3 > >>