Returns a new Array formed from self
with elements rotated from one end to the other.
When no argument given, returns a new Array that is like self
, except that the first element has been rotated to the last position:
a = [:foo, 'bar', 2, 'bar'] a1 = a.rotate a1 # => ["bar", 2, "bar", :foo]
When given a non-negative Integer
count
, returns a new Array with count
elements rotated from the beginning to the end:
a = [:foo, 'bar', 2] a1 = a.rotate(2) a1 # => [2, :foo, "bar"]
If count
is large, uses count % array.size
as the count:
a = [:foo, 'bar', 2] a1 = a.rotate(20) a1 # => [2, :foo, "bar"]
If count
is zero, returns a copy of self
, unmodified:
a = [:foo, 'bar', 2] a1 = a.rotate(0) a1 # => [:foo, "bar", 2]
When given a negative Integer
count
, rotates in the opposite direction, from end to beginning:
a = [:foo, 'bar', 2] a1 = a.rotate(-2) a1 # => ["bar", 2, :foo]
If count
is small (far from zero), uses count % array.size
as the count:
a = [:foo, 'bar', 2] a1 = a.rotate(-5) a1 # => ["bar", 2, :foo]
Rotates self
in place by moving elements from one end to the other; returns self
.
When no argument given, rotates the first element to the last position:
a = [:foo, 'bar', 2, 'bar'] a.rotate! # => ["bar", 2, "bar", :foo]
When given a non-negative Integer
count
, rotates count
elements from the beginning to the end:
a = [:foo, 'bar', 2] a.rotate!(2) a # => [2, :foo, "bar"]
If count
is large, uses count % array.size
as the count:
a = [:foo, 'bar', 2] a.rotate!(20) a # => [2, :foo, "bar"]
If count
is zero, returns self
unmodified:
a = [:foo, 'bar', 2] a.rotate!(0) a # => [:foo, "bar", 2]
When given a negative Integer
count
, rotates in the opposite direction, from end to beginning:
a = [:foo, 'bar', 2] a.rotate!(-2) a # => ["bar", 2, :foo]
If count
is small (far from zero), uses count % array.size
as the count:
a = [:foo, 'bar', 2] a.rotate!(-5) a # => ["bar", 2, :foo]
Returns the first element in self
that is an Array whose first element ==
obj
:
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] a.assoc(4) # => [4, 5, 6]
Returns nil
if no such element is found.
Related: rassoc
.
Returns the first element in self
that is an Array whose second element ==
obj
:
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] a.rassoc(4) # => [2, 4]
Returns nil
if no such element is found.
Related: assoc
.
Returns a new Array containing all but the first n
element of self
, where n
is a non-negative Integer
; does not modify self
.
Examples:
a = [0, 1, 2, 3, 4, 5] a.drop(0) # => [0, 1, 2, 3, 4, 5] a.drop(1) # => [1, 2, 3, 4, 5] a.drop(2) # => [2, 3, 4, 5]
Prepends the given objects
to self
:
a = [:foo, 'bar', 2] a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
Returns the predecessor of self
(equivalent to self - 1
):
1.pred #=> 0 -1.pred #=> -2
Related: Integer#succ
(successor value).
Returns self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.round(-1) # => 560 555.round(-2) # => 600 555.round(-3) # => 1000 -555.round(-2) # => -600 555.round(-4) # => 0
Returns self
when ndigits
is zero or positive.
555.round # => 555 555.round(1) # => 555 555.round(50) # => 555
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:
25.round(-1, half: :up) # => 30 (-25).round(-1, half: :up) # => -30
:down
: round toward zero:
25.round(-1, half: :down) # => 20 (-25).round(-1, half: :down) # => -20
:even
: round toward the candidate whose last nonzero digit is even:
25.round(-1, half: :even) # => 20 15.round(-1, half: :even) # => 20 (-25).round(-1, half: :even) # => -20
Raises and exception if the value for half
is invalid.
Related: Integer#truncate
.
Returns true
if self
has a zero value, false
otherwise.
Returns true
if zero
has a zero value, false
otherwise.
Of the Core and Standard Library classes, only Rational
and Complex
use this implementation.
Returns self
if self
is not a zero value, nil
otherwise; uses method zero?
for the evaluation.
The returned self
allows the method to be chained:
a = %w[z Bb bB bb BB a aA Aa AA A] a.sort {|a, b| (a.downcase <=> b.downcase).nonzero? || a <=> b } # => ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
Of the Core and Standard Library classes, Integer
, Float
, Rational
, and Complex
use this implementation.
Returns self
rounded to the nearest value with a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#round
.
Interprets the leading substring of self
as a string of octal digits (with an optional sign) and returns the corresponding number; returns zero if there is no such leading substring:
'123'.oct # => 83 '-377'.oct # => -255 '0377non-numeric'.oct # => 255 'non-numeric'.oct # => 0
If self
starts with 0
, radix indicators are honored; see Kernel#Integer
.
Related: String#hex
.
Prepends each string in other_strings
to self
and returns self
:
s = 'foo' s.prepend('bar', 'baz') # => "barbazfoo" s # => "barbazfoo"
Related: String#concat
.
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 true
if self
is 0.0, false
otherwise.
Forces the fiber to be blocking for the duration of the block. Returns the result of the block.
See the “Non-blocking fibers” section in class docs for details.
Returns true
if fiber
is blocking and false
otherwise. Fiber
is non-blocking if it was created via passing blocking: false
to Fiber.new
, or via Fiber.schedule
.
Note that, even if the method returns false
, the fiber behaves differently only if Fiber.scheduler
is set in the current thread.
See the “Non-blocking fibers” section in class docs for details.
Returns false
if the current fiber is non-blocking. Fiber
is non-blocking if it was created via passing blocking: false
to Fiber.new
, or via Fiber.schedule
.
If the current Fiber
is blocking, the method returns 1. Future developments may allow for situations where larger integers could be returned.
Note that, even if the method returns false
, Fiber
behaves differently only if Fiber.scheduler
is set in the current thread.
See the “Non-blocking fibers” section in class docs for details.
Changes the root directory of the calling process to that specified in dirpath
. The new root directory is used for pathnames beginning with '/'
. The root directory is inherited by all children of the calling process.
Only a privileged process may call chroot
.
See Linux chroot.
Locks or unlocks a file according to the given locking_constant
, a bitwise OR of the values in the table below.
Not available on all platforms.
Returns false
if File::LOCK_NB
is specified and the operation would have blocked; otherwise returns 0
.
Locking Constants | ||
---|---|---|
Constant | Lock | Effect |
File::LOCK_EX | Exclusive | Only one process may hold an exclusive lock for self at a time. |
File::LOCK_NB | Non-blocking | No blocking; may be combined with other File::LOCK_SH or File::LOCK_EX using the bitwise OR operator |. |
File::LOCK_SH | Shared | Multiple processes may each hold a shared lock for self at the same time. |
File::LOCK_UN | Unlock | Remove an existing lock held by this process. |
Example:
# Update a counter using an exclusive lock. # Don't use File::WRONLY because it truncates the file. File.open('counter', File::RDWR | File::CREAT, 0644) do |f| f.flock(File::LOCK_EX) value = f.read.to_i + 1 f.rewind f.write("#{value}\n") f.flush f.truncate(f.pos) end # Read the counter using a shared lock. File.open('counter', 'r') do |f| f.flock(File::LOCK_SH) f.read end