るりまサーチ

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

別のキーワード

  1. _builtin *
  2. matrix *
  3. vector *
  4. array *
  5. bigdecimal *

クラス

モジュール

キーワード

検索結果

String#count(*chars) -> Integer (18233.0)

chars で指定された文字が文字列 self にいくつあるか数えます。

...文字のパターン

//emlist[例][ruby]{
p 'abcdefg'.count('c') # => 1
p '123456789'.count('2378') # => 4
p '123456789'.count('2-8', '^4-6') # => 4

# ファイルの行数を数える
n_lines = File.read("foo").count("\n")

# ファイルの末尾に改行コードがな...
...い場合にも対処する
buf = File.read("foo")
n_lines = buf.count("\n")
n_lines += 1 if /[^\n]\z/ =~ buf
# if /\n\z/ !~ buf だと空ファイルを 1 行として数えてしまうのでダメ
//}...

ObjectSpace.#each_object -> Enumerator (80.0)

指定された klass と Object#kind_of? の関係にある全ての オブジェクトに対して繰り返します。引数が省略された時には全てのオブ ジェクトに対して繰り返します。 繰り返した数を返します。

...
Enumerator オブジェクトを返します。

次のクラスのオブジェクトについては繰り返しません

*
Fixnum
*
Symbol
*
TrueClass
*
FalseClass
*
NilClass

とくに、klass に Fixnum や Symbol などのクラスを指定した場合は、
何も繰り返さないこ...
...を扱う][ruby]{
ObjectSpace.each_object.take(5).each { |x| p x }
count
= ObjectSpace.each_object { |x| x }
puts "Total count: #{count}"

# => "scope"
# => "scopes"
# => "sym"
# => "class_names"
# => "@corrections"
# => Total count: 9938
//}

//emlist[例: 任意のクラスを扱う][ruby]{
Person...
...= Struct.new(:name)
s1 = Person.new("tanaka")
s2 = Person.new("sato")

count
= ObjectSpace.each_object(Person) { |x| p x }
puts "Total count: #{count}"

# => #<struct Person name="sato">
# => #<struct Person name="tanaka">
# => Total count: 2
//}...

ObjectSpace.#each_object {|object| ...} -> Integer (80.0)

指定された klass と Object#kind_of? の関係にある全ての オブジェクトに対して繰り返します。引数が省略された時には全てのオブ ジェクトに対して繰り返します。 繰り返した数を返します。

...
Enumerator オブジェクトを返します。

次のクラスのオブジェクトについては繰り返しません

*
Fixnum
*
Symbol
*
TrueClass
*
FalseClass
*
NilClass

とくに、klass に Fixnum や Symbol などのクラスを指定した場合は、
何も繰り返さないこ...
...を扱う][ruby]{
ObjectSpace.each_object.take(5).each { |x| p x }
count
= ObjectSpace.each_object { |x| x }
puts "Total count: #{count}"

# => "scope"
# => "scopes"
# => "sym"
# => "class_names"
# => "@corrections"
# => Total count: 9938
//}

//emlist[例: 任意のクラスを扱う][ruby]{
Person...
...= Struct.new(:name)
s1 = Person.new("tanaka")
s2 = Person.new("sato")

count
= ObjectSpace.each_object(Person) { |x| p x }
puts "Total count: #{count}"

# => #<struct Person name="sato">
# => #<struct Person name="tanaka">
# => Total count: 2
//}...

ObjectSpace.#each_object(klass) -> Enumerator (80.0)

指定された klass と Object#kind_of? の関係にある全ての オブジェクトに対して繰り返します。引数が省略された時には全てのオブ ジェクトに対して繰り返します。 繰り返した数を返します。

...
Enumerator オブジェクトを返します。

次のクラスのオブジェクトについては繰り返しません

*
Fixnum
*
Symbol
*
TrueClass
*
FalseClass
*
NilClass

とくに、klass に Fixnum や Symbol などのクラスを指定した場合は、
何も繰り返さないこ...
...を扱う][ruby]{
ObjectSpace.each_object.take(5).each { |x| p x }
count
= ObjectSpace.each_object { |x| x }
puts "Total count: #{count}"

# => "scope"
# => "scopes"
# => "sym"
# => "class_names"
# => "@corrections"
# => Total count: 9938
//}

//emlist[例: 任意のクラスを扱う][ruby]{
Person...
...= Struct.new(:name)
s1 = Person.new("tanaka")
s2 = Person.new("sato")

count
= ObjectSpace.each_object(Person) { |x| p x }
puts "Total count: #{count}"

# => #<struct Person name="sato">
# => #<struct Person name="tanaka">
# => Total count: 2
//}...

ObjectSpace.#each_object(klass) {|object| ...} -> Integer (80.0)

指定された klass と Object#kind_of? の関係にある全ての オブジェクトに対して繰り返します。引数が省略された時には全てのオブ ジェクトに対して繰り返します。 繰り返した数を返します。

...
Enumerator オブジェクトを返します。

次のクラスのオブジェクトについては繰り返しません

*
Fixnum
*
Symbol
*
TrueClass
*
FalseClass
*
NilClass

とくに、klass に Fixnum や Symbol などのクラスを指定した場合は、
何も繰り返さないこ...
...を扱う][ruby]{
ObjectSpace.each_object.take(5).each { |x| p x }
count
= ObjectSpace.each_object { |x| x }
puts "Total count: #{count}"

# => "scope"
# => "scopes"
# => "sym"
# => "class_names"
# => "@corrections"
# => Total count: 9938
//}

//emlist[例: 任意のクラスを扱う][ruby]{
Person...
...= Struct.new(:name)
s1 = Person.new("tanaka")
s2 = Person.new("sato")

count
= ObjectSpace.each_object(Person) { |x| p x }
puts "Total count: #{count}"

# => #<struct Person name="sato">
# => #<struct Person name="tanaka">
# => Total count: 2
//}...

絞り込み条件を変える

BasicObject#! -> bool (38.0)

オブジェクトを真偽値として評価し、その論理否定を返します。

...ass NegationRecorder < BasicObject
def initialize
@count = 0
end
attr_reader :count

def !
@count += 1
super
end
end

recorder = NegationRecorder.new
!recorder
!!!!!!!recorder
puts 'hoge' if !recorder

puts recorder.count #=> 3
//}

//emlist[例][ruby]{
class AnotherFalse < Ba...
...sicObject
def !
true
end
end
another_false = AnotherFalse.new

# another_falseは**
puts "another false is a truth" if another_false
#=> "another false is a truth"
//}...

Proc#<<(callable) -> Proc (26.0)

self と引数を合成した Proc を返します。

...e Proc、Method、もしくは任意の call メソッドを持ったオブジェクト。

//emlist[例][ruby]{
f = proc { |x| x * x }
g = proc { |x| x + x }

# (3 + 3) * (3 + 3)
p (f << g).call(3) # => 36
//}

//emlist[call を定義したオブジェクトを渡す例][ruby]{
class WordScanner...
...def self.call(str)
str.scan(/\w+/)
end
end

File.write('testfile', <<~TEXT)
Hello, World!
Hello, Ruby!
TEXT

pipeline = proc { |data| puts "word count: #{data.size}" } << WordScanner << File.method(:read)
pipeline.call('testfile') # => word count: 4
//}

@see Method#<<, Method#>>...

Method#arity -> Integer (20.0)

メソッドが受け付ける引数の数を返します。

...します。

//emlist[例][ruby]{
class C
def u; end
def v(a); end
def w(*a); end
def x(a, b); end
def y(a, b, *c); end
def z(a, b, *c, &d); end
end

c = C.new
p c.method(:u).arity #=> 0
p c.method(:v).arity #=> 1
p c.method(:w).ari...
...ty #=> -1
p c.method(:x).arity #=> 2
p c.method(:y).arity #=> -3
p c.method(:z).arity #=> -3

s = "xyz"
s.method(:size).arity #=> 0
s.method(:replace).arity #=> 1
s.method(:squeeze).arity #=> -1
s.method(:count).arity #=> -1
//}...

UnboundMethod#arity -> Integer (20.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(:two).arity #=> 1
p C.instance...
...).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
//}...