Returns the total number of characters in self
that are specified by the given selectors
(see Multiple Character Selectors):
a = "hello world" a.count "lo" #=> 5 a.count "lo", "o" #=> 2 a.count "hello", "^l" #=> 4 a.count "ej-m" #=> 4 "hello^world".count "\\^aeiou" #=> 4 "hello-world".count "a\\-eo" #=> 4 c = "hello world\\r\\n" c.count "\\" #=> 2 c.count "\\A" #=> 0 c.count "X-\\w" #=> 3
Returns self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is non-negative, returns a float with ndigits
after the decimal point (as available):
f = 12345.6789 f.round(1) # => 12345.7 f.round(3) # => 12345.679 f = -12345.6789 f.round(1) # => -12345.7 f.round(3) # => -12345.679
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.round(0) # => 12346 f.round(-3) # => 12000 f = -12345.6789 f.round(0) # => -12346 f.round(-3) # => -12000
If keyword argument half
is given, and self
is equidistant from the two candidate values, the rounding is according to the given half
value:
:up
or nil
: round away from zero:
2.5.round(half: :up) # => 3 3.5.round(half: :up) # => 4 (-2.5).round(half: :up) # => -3
:down
: round toward zero:
2.5.round(half: :down) # => 2 3.5.round(half: :down) # => 3 (-2.5).round(half: :down) # => -2
:even
: round toward the candidate whose last nonzero digit is even:
2.5.round(half: :even) # => 2 3.5.round(half: :even) # => 4 (-2.5).round(half: :even) # => -2
Raises and exception if the value for half
is invalid.
Related: Float#truncate
.
Returns self
truncated (toward zero) to a precision of ndigits
decimal digits.
When ndigits
is positive, returns a float with ndigits
digits after the decimal point (as available):
f = 12345.6789 f.truncate(1) # => 12345.6 f.truncate(3) # => 12345.678 f = -12345.6789 f.truncate(1) # => -12345.6 f.truncate(3) # => -12345.678
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.truncate(0) # => 12345 f.truncate(-3) # => 12000 f = -12345.6789 f.truncate(0) # => -12345 f.truncate(-3) # => -12000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
Related: Float#round
.
Returns:
1, if self
is Infinity
.
-1 if self
is -Infinity
.
nil
, otherwise.
Examples:
f = 1.0/0.0 # => Infinity f.infinite? # => 1 f = -1.0/0.0 # => -Infinity f.infinite? # => -1 f = 1.0 # => 1.0 f.infinite? # => nil f = 0.0/0.0 # => NaN f.infinite? # => nil
Returns true
if self
is not Infinity
, -Infinity
, or NaN
, false
otherwise:
f = 2.0 # => 2.0 f.finite? # => true f = 1.0/0.0 # => Infinity f.finite? # => false f = -1.0/0.0 # => -Infinity f.finite? # => false f = 0.0/0.0 # => NaN f.finite? # => false
Removes the directory at dirpath
from the underlying file system:
Dir.rmdir('foo') # => 0
Raises an exception if the directory is not empty.
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. Since the underlying implementation relies on the unlink(2)
system call, the type of exception raised depends on its error type (see linux.die.net/man/2/unlink) and has the form of e.g. Errno::ENOENT.
See also Dir::rmdir
.
Truncates the file file_name to be at most integer bytes long. Not available on all platforms.
f = File.new("out", "w") f.write("1234567890") #=> 10 f.close #=> nil File.truncate("out", 5) #=> 0 File.size("out") #=> 5
Truncates file to at most integer bytes. The file must be opened for writing. Not available on all platforms.
f = File.new("out", "w") f.syswrite("1234567890") #=> 10 f.truncate(5) #=> 0 f.close() #=> nil File.size("out") #=> 5
Returns true
if self
is a Sunday, false
otherwise.
Returns false
Returns a new Time
object whose numeric value is that of self
, with its seconds value rounded to precision ndigits
:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r) t # => 2010-03-30 05:43:25.123456789 UTC t.round # => 2010-03-30 05:43:25 UTC t.round(0) # => 2010-03-30 05:43:25 UTC t.round(1) # => 2010-03-30 05:43:25.1 UTC t.round(2) # => 2010-03-30 05:43:25.12 UTC t.round(3) # => 2010-03-30 05:43:25.123 UTC t.round(4) # => 2010-03-30 05:43:25.1235 UTC t = Time.utc(1999, 12,31, 23, 59, 59) t # => 1999-12-31 23:59:59 UTC (t + 0.4).round # => 1999-12-31 23:59:59 UTC (t + 0.49).round # => 1999-12-31 23:59:59 UTC (t + 0.5).round # => 2000-01-01 00:00:00 UTC (t + 1.4).round # => 2000-01-01 00:00:00 UTC (t + 1.49).round # => 2000-01-01 00:00:00 UTC (t + 1.5).round # => 2000-01-01 00:00:01 UTC
Related: Time#ceil
, Time#floor
.
Returns true
if self
represents a Sunday, false
otherwise:
t = Time.utc(2000, 1, 2) # => 2000-01-02 00:00:00 UTC t.sunday? # => true
Related: Time#monday?
, Time#tuesday?
, Time#wednesday?
.
Pushes back (“unshifts”) the given data onto the stream’s buffer, placing the data so that it is next to be read; returns nil
. See Byte IO.
Note that:
Calling the method has no effect with unbuffered reads (such as IO#sysread
).
Calling rewind
on the stream discards the pushed-back data.
When argument integer
is given, uses only its low-order byte:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetbyte(0x41) # => nil f.read # => "A012" f.rewind f.ungetbyte(0x4243) # => nil f.read # => "C012" f.close
When argument string
is given, uses all bytes:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetbyte('A') # => nil f.read # => "A012" f.rewind f.ungetbyte('BCDE') # => nil f.read # => "BCDE012" f.close
Pushes back (“unshifts”) the given data onto the stream’s buffer, placing the data so that it is next to be read; returns nil
. See Character IO.
Note that:
Calling the method has no effect with unbuffered reads (such as IO#sysread
).
Calling rewind
on the stream discards the pushed-back data.
When argument integer
is given, interprets the integer as a character:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetc(0x41) # => nil f.read # => "A012" f.rewind f.ungetc(0x0442) # => nil f.getc.ord # => 1090 f.close
When argument string
is given, uses all characters:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetc('A') # => nil f.read # => "A012" f.rewind f.ungetc("\u0442\u0435\u0441\u0442") # => nil f.getc.ord # => 1090 f.getc.ord # => 1077 f.getc.ord # => 1089 f.getc.ord # => 1090 f.close
Returns the count of elements, based on an argument or block criterion, if given.
With no argument and no block given, returns the number of elements:
(1..4).count # => 4 (1...4).count # => 3 ('a'..'d').count # => 4 ('a'...'d').count # => 3 (1..).count # => Infinity (..4).count # => Infinity
With argument object
, returns the number of object
found in self
, which will usually be zero or one:
(1..4).count(2) # => 1 (1..4).count(5) # => 0 (1..4).count('a') # => 0
With a block given, calls the block with each element; returns the number of elements for which the block returns a truthy value:
(1..4).count {|element| element < 3 } # => 2
Related: Range#size
.
Returns rat
truncated (toward zero) to a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns a rational when ndigits
is positive, otherwise returns an integer.
Rational(3).truncate #=> 3 Rational(2, 3).truncate #=> 0 Rational(-3, 2).truncate #=> -1 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').truncate(+1).to_f #=> -123.4 Rational('-123.456').truncate(-1) #=> -120
Returns rat
rounded to the nearest value with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns a rational when ndigits
is positive, otherwise returns an integer.
Rational(3).round #=> 3 Rational(2, 3).round #=> 1 Rational(-3, 2).round #=> -2 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').round(+1).to_f #=> -123.5 Rational('-123.456').round(-1) #=> -120
The optional half
keyword argument is available similar to Float#round
.
Rational(25, 100).round(1, half: :up) #=> (3/10) Rational(25, 100).round(1, half: :down) #=> (1/5) Rational(25, 100).round(1, half: :even) #=> (1/5) Rational(35, 100).round(1, half: :up) #=> (2/5) Rational(35, 100).round(1, half: :down) #=> (3/10) Rational(35, 100).round(1, half: :even) #=> (2/5) Rational(-25, 100).round(1, half: :up) #=> (-3/10) Rational(-25, 100).round(1, half: :down) #=> (-1/5) Rational(-25, 100).round(1, half: :even) #=> (-1/5)
Returns true
if self
points to a mountpoint.
Removes a file or directory, using File.unlink
if self
is a file, or Dir.unlink
as necessary.