315件ヒット
[1-100件を表示]
(0.068秒)
クラス
モジュール
- Enumerable (155)
検索結果
先頭5件
-
Bignum
# **(other) -> Fixnum | Bignum | Float (26323.0) -
算術演算子。冪(べき乗)を計算します。
...算術演算子。冪(べき乗)を計算します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
2 ** 3 # => 8
2 ** 0 # => 1
0 ** 0 # => 1... -
Fixnum
# **(other) -> Fixnum | Bignum | Float (26323.0) -
算術演算子。冪(べき乗)を計算します。
...算術演算子。冪(べき乗)を計算します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
2 ** 3 # => 8
2 ** 0 # => 1
0 ** 0 # => 1... -
Rational
# **(other) -> Rational | Float (26228.0) -
冪(べき)乗を計算します。
...す。other が有理数であっても、計算結果が無理数だった場合は Float
を返します。
//emlist[例][ruby]{
r = Rational(3, 4)
r ** Rational(2, 1) # => (9/16)
r ** 2 # => (9/16)
r ** 2.0 # => 0.5625
r ** Rational(1, 2) # => 0.866025403784439
//}... -
Enumerable
# max _ by {|item| . . . } -> object | nil (8154.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...ator: ["albatross", "dog", "horse"]: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......{|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 (8154.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...ator: ["albatross", "dog", "horse"]: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......{|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] }
#=> *
# ***
# ******......# ***********
# ******************
# *****************************
# *****************************************
# ****************************************************
# ***************************************************************
# ****************************************************... -
Enumerator
:: Lazy # enum _ for(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (8135.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...if n < 0
if block_given?
each do |*val|
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]... -
Enumerator
:: Lazy # to _ enum(method = :each , *args) {|*args| block} -> Enumerator :: Lazy (8135.0) -
Object#to_enum と同じですが、Enumerator::Lazy を返します。
...if n < 0
if block_given?
each do |*val|
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]... -
Object
# then {|x| . . . } -> object (8135.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...引数としてブロックを評価し、ブロックの結果を返します。
//emlist[例][ruby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い......ソッドチェインのパイプライン][ruby]{
require 'open-uri'
require 'json'
construct_url(arguments).
yield_self {|url| URI(url).read }.
yield_self {|response| JSON.parse(response) }
//}
ブロックなしで呼び出されたときは Enumerator を返します。
例えば条件に... -
Object
# yield _ self {|x| . . . } -> object (8135.0) -
self を引数としてブロックを評価し、ブロックの結果を返します。
...してブロックを評価し、ブロックの結果を返します。
//emlist[例][ruby]{
"my string".yield_self {|s| s.upcase } # => "MY STRING"
3.next.yield_self {|x| x**x }.to_s # => "256"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方......ソッドチェインのパイプライン][ruby]{
require 'open-uri'
require 'json'
construct_url(arguments).
yield_self {|url| URI(url).read }.
yield_self {|response| JSON.parse(response) }
//}
ブロックなしで呼び出されたときは Enumerator を返します。
例えば条件に......引数としてブロックを評価し、ブロックの結果を返します。
//emlist[例][ruby]{
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
//}
値をメソッドチェインのパイプラインに次々と渡すのは良い... -
Enumerable
# inject(init = self . first) {|result , item| . . . } -> object (8129.0) -
リストのたたみこみ演算を行います。
...例][ruby]{
# 合計を計算する。
p [2, 3, 4, 5].inject {|result, item| result + item } #=> 14
# 自乗和を計算する。初期値をセットする必要がある。
p [2, 3, 4, 5].inject(0) {|result, item| result + item**2 } #=> 54
//}
この式は以下のように書いても......同じ結果が得られます。
//emlist[例][ruby]{
result = 0
[1, 2, 3, 4, 5].each {|v| result += v }
p result # => 15
p [1, 2, 3, 4, 5].inject(:+) #=> 15
p ["b", "c", "d"].inject("abbccddde", :squeeze) #=> "abcde"
//}... -
Enumerable
# reduce(init = self . first) {|result , item| . . . } -> object (8129.0) -
リストのたたみこみ演算を行います。
...例][ruby]{
# 合計を計算する。
p [2, 3, 4, 5].inject {|result, item| result + item } #=> 14
# 自乗和を計算する。初期値をセットする必要がある。
p [2, 3, 4, 5].inject(0) {|result, item| result + item**2 } #=> 54
//}
この式は以下のように書いても......同じ結果が得られます。
//emlist[例][ruby]{
result = 0
[1, 2, 3, 4, 5].each {|v| result += v }
p result # => 15
p [1, 2, 3, 4, 5].inject(:+) #=> 15
p ["b", "c", "d"].inject("abbccddde", :squeeze) #=> "abcde"
//}... -
Enumerable
# uniq { |item| . . . } -> Array (8123.0) -
self から重複した値を取り除いた配列を返します。
...][ruby]{
olympics = {
1896 => 'Athens',
1900 => 'Paris',
1904 => 'Chicago',
1906 => 'Athens',
1908 => 'Rome',
}
olympics.uniq{|k,v| v} # => [[1896, "Athens"], [1900, "Paris"], [1904, "Chicago"], [1908, "Rome"]]
(1..100).uniq{|x| (x**2) % 10 } # => [1, 2, 3, 4, 5, 10]
//}
@see Array#uniq...