171件ヒット
[1-100件を表示]
(0.075秒)
クラス
- Array (11)
- BasicObject (22)
- Method (11)
- Proc (6)
- Thread (22)
- UnboundMethod (33)
モジュール
- Enumerable (55)
- GC (11)
検索結果
先頭5件
-
Enumerable
# count {|obj| . . . } -> Integer (29230.0) -
レシーバの要素数を返します。
...価して真になった要素の個数を
カウントして返します。
@param item カウント対象となる値。
//emlist[例][ruby]{
enum = [1, 2, 4, 2].each
enum.count # => 4
enum.count(2) # => 2
enum.count{|x|x%2==0} # => 3
//}
@see Array#count... -
Enumerable
# count -> Integer (29130.0) -
レシーバの要素数を返します。
...価して真になった要素の個数を
カウントして返します。
@param item カウント対象となる値。
//emlist[例][ruby]{
enum = [1, 2, 4, 2].each
enum.count # => 4
enum.count(2) # => 2
enum.count{|x|x%2==0} # => 3
//}
@see Array#count... -
Enumerable
# count(item) -> Integer (29130.0) -
レシーバの要素数を返します。
...価して真になった要素の個数を
カウントして返します。
@param item カウント対象となる値。
//emlist[例][ruby]{
enum = [1, 2, 4, 2].each
enum.count # => 4
enum.count(2) # => 2
enum.count{|x|x%2==0} # => 3
//}
@see Array#count... -
Array
# count {|obj| . . . } -> Integer (26230.0) -
レシーバの要素数を返します。
...ブロックを評価して真になった要素の個数を
カウントして返します。
@param item カウント対象となる値。
//emlist[例][ruby]{
ary = [1, 2, 4, 2.0]
ary.count # => 4
ary.count(2) # => 2
ary.count{|x|x%2==0} # => 3
//}
@see Enumerable#count... -
Enumerable
# sort _ by -> Enumerator (14163.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...[例][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 を使......[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] (14163.0) -
ブロックの評価結果を <=> メソッドで比較することで、self を昇 順にソートします。ソートされた配列を新たに生成して返します。
...[例][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 を使......[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 # =... -
GC
# garbage _ collect(full _ mark: true , immediate _ sweep: true) -> nil (14115.0) -
ガーベージコレクトを開始します。
...ガーベージコレクトを開始します。
GC.start や ObjectSpace.#garbage_collect と同じ働きをします。
GC.disable により GC が禁止されている場合は何もしません。
nil を返します。
@param full_mark マイナー GC を動作させる場合は false を......Ruby の実装やバージョンによって異なりま
す。将来のバージョンとの互換性も保証されません。また、Ruby の実装がサポー
トしていない場合はキーワード引数を指定しても無視される可能性があります。
//emlist[例][ruby]{
inc......lude GC
GC.count # => 3
garbage_collect
GC.count # => 4
//}......ガーベージコレクトを開始します。
GC.start や ObjectSpace.#garbage_collect と同じ働きをします。
GC.disable により GC が禁止されている場合でもガベージコレクトを開始します。
nil を返します。
@param full_mark マイナー GC を動作さ... -
BasicObject
# ! -> bool (11127.0) -
オブジェクトを真偽値として評価し、その論理否定を返します。
...Ruby の制御式において nil や false 以外が偽として
扱われることはありません。
@return オブジェクトが偽であれば真、さもなくば偽
//emlist[例][ruby]{
class NegationRecorder < BasicObject
def initialize
@count = 0
end
attr_reader :count
d......ef !
@count += 1
super
end
end
recorder = NegationRecorder.new
!recorder
!!!!!!!recorder
puts 'hoge' if !recorder
puts recorder.count #=> 3
//}
//emlist[例][ruby]{
class AnotherFalse < BasicObject
def !
true
end
end
another_false = AnotherFalse.new
# another_falseは*真*
puts... -
BasicObject
# !=(other) -> bool (11127.0) -
オブジェクトが other と等しくないことを判定します。
...を論理否定して返します。
このため、サブクラスで BasicObject#== を再定義しても != とは自動的に整合性が
とれるようになっています。
ただし、 BasicObject#!= 自身や BasicObject#! を再定義した際には、ユーザーの責任で
整合性......@param other 比較対象となるオブジェクト
@see BasicObject#==, BasicObject#!
//emlist[例][ruby]{
class NonequalityRecorder < BasicObject
def initialize
@count = 0
end
attr_reader :count
def !=(other)
@count += 1
super
end
end
recorder = NonequalityRecorder.new......recorder != 1
puts 'hoge' if recorder != "str"
p recorder.count #=> 2
//}... -
UnboundMethod
# arity -> Integer (11027.0) -
メソッドが受け付ける引数の数を返します。
...可変長引数を
受け付ける場合、-1 を返します。
//emlist[例][ruby]{
class C
def one; end
def two(a); end
def three(*a); end
def four(a, b); end
def five(a, b, *c); end
def six(a, b, *c, &d); end
end
p C.instance_method(:one).arity #=> 0
p C.instance_method(......).arity #=> 2
p C.instance_method(:five).arity #=> -3
p C.instance_method(:six).arity #=> -3
String.instance_method(:size).arity #=> 0
String.instance_method(:replace).arity #=> 1
String.instance_method(:squeeze).arity #=> -1
String.instance_method(:count).arity #=> -1
//}...