Returns a new array formed from self
with elements rotated from one end to the other.
With non-negative numeric count
, rotates elements from the beginning to the end:
[0, 1, 2, 3].rotate(2) # => [2, 3, 0, 1] [0, 1, 2, 3].rotate(2.1) # => [2, 3, 0, 1]
If count
is large, uses count % array.size
as the count:
[0, 1, 2, 3].rotate(22) # => [2, 3, 0, 1]
With a count
of zero, rotates no elements:
[0, 1, 2, 3].rotate(0) # => [0, 1, 2, 3]
With negative numeric count
, rotates in the opposite direction, from the end to the beginning:
[0, 1, 2, 3].rotate(-1) # => [3, 0, 1, 2]
If count
is small (far from zero), uses count % array.size
as the count:
[0, 1, 2, 3].rotate(-21) # => [3, 0, 1, 2]
Related: see Methods for Fetching.
Rotates self
in place by moving elements from one end to the other; returns self
.
With non-negative numeric count
, rotates count
elements from the beginning to the end:
[0, 1, 2, 3].rotate!(2) # => [2, 3, 0, 1] [0, 1, 2, 3].rotate!(2.1) # => [2, 3, 0, 1]
If count
is large, uses count % array.size
as the count:
[0, 1, 2, 3].rotate!(21) # => [1, 2, 3, 0]
If count
is zero, rotates no elements:
[0, 1, 2, 3].rotate!(0) # => [0, 1, 2, 3]
With a negative numeric count
, rotates in the opposite direction, from end to beginning:
[0, 1, 2, 3].rotate!(-1) # => [3, 0, 1, 2]
If count
is small (far from zero), uses count % array.size
as the count:
[0, 1, 2, 3].rotate!(-21) # => [3, 0, 1, 2]
Related: see Methods for Assigning.
Returns a new array containing only the non-nil
elements from self
; element order is preserved:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact # => [0, false, "", [], {}]
Related: Array#compact!
; see also Methods for Deleting.
Removes all nil
elements from self
; Returns self
if any elements are removed, nil
otherwise:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact! # => [0, false, "", [], {}] a # => [0, false, "", [], {}] a.compact! # => nil
Related: Array#compact
; see also Methods for Deleting.
Returns a new array that is a recursive flattening of self
to depth
levels of recursion; depth
must be an integer-convertible object or nil
. At each level of recursion:
Each element that is an array is “flattened” (that is, replaced by its individual array elements).
Each element that is not an array is unchanged (even if the element is an object that has instance method flatten
).
With non-negative integer argument depth
, flattens recursively through depth
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ] a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(0) # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(1 ) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
With nil
or negative depth
, flattens all levels.
a.flatten # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
Related: Array#flatten!
; see also Methods for Converting.
Returns self
as a recursively flattening of self
to depth
levels of recursion; depth
must be an integer-convertible object, or nil
. At each level of recursion:
Each element that is an array is “flattened” (that is, replaced by its individual array elements).
Each element that is not an array is unchanged (even if the element is an object that has instance method flatten
).
Returns nil
if no elements were flattened.
With non-negative integer argument depth
, flattens recursively through depth
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ] a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
With nil
or negative argument depth
, flattens all levels:
a.dup.flatten! # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
Related: Array#flatten
; see also Methods for Assigning.
Iterates over permutations of the elements of self
; the order of permutations is indeterminate.
With a block and an in-range positive integer argument count
(0 < count <= self.size
) given, calls the block with each permutation of self
of size count
; returns self
:
a = [0, 1, 2] perms = [] a.permutation(1) {|perm| perms.push(perm) } perms # => [[0], [1], [2]] perms = [] a.permutation(2) {|perm| perms.push(perm) } perms # => [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]] perms = [] a.permutation(3) {|perm| perms.push(perm) } perms # => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
When count
is zero, calls the block once with a new empty array:
perms = [] a.permutation(0) {|perm| perms.push(perm) } perms # => [[]]
When count
is out of range (negative or larger than self.size
), does not call the block:
a.permutation(-1) {|permutation| fail 'Cannot happen' } a.permutation(4) {|permutation| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: Methods for Iterating.
Returns random elements from self
, as selected by the object given by the keyword argument random
.
With no argument count
given, returns one random element from self
:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.sample # => 3 a.sample # => 8
Returns nil
if self
is empty:
[].sample # => nil
With a non-negative numeric argument count
given, returns a new array containing count
random elements from self
:
a.sample(3) # => [8, 9, 2] a.sample(6) # => [9, 6, 0, 3, 1, 4]
The order of the result array is unrelated to the order of self
.
Returns a new empty Array
if self
is empty:
[].sample(4) # => []
May return duplicates in self
:
a = [1, 1, 1, 2, 2, 3] a.sample(a.size) # => [1, 1, 3, 2, 1, 2]
Returns no more than a.size
elements (because no new duplicates are introduced):
a.sample(50) # => [6, 4, 1, 8, 5, 9, 0, 2, 3, 7]
The object given with the keyword argument random
is used as the random number generator:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample(random: Random.new(1)) # => 6 a.sample(4, random: Random.new(1)) # => [6, 10, 9, 2]
Related: see Methods for Fetching.
Formats each element in self
into a binary string; returns that string. See Packed Data.
Returns a Hash
containing implementation-dependent counters inside the VM.
This hash includes information about method/constant caches:
{ :constant_cache_invalidations=>2, :constant_cache_misses=>14, :global_cvar_state=>27 }
If USE_DEBUG_COUNTER
is enabled, debug counters will be included.
The contents of the hash are implementation specific and may be changed in the future.
This method is only expected to work on C Ruby.
Returns self
truncated (toward zero) to a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.truncate(-1) # => 550 555.truncate(-2) # => 500 -555.truncate(-2) # => -500
Returns self
when ndigits
is zero or positive.
555.truncate # => 555 555.truncate(50) # => 555
Related: Integer#round
.
Calls the given block self
times with each integer in (0..self-1)
:
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator
.
Returns the imaginary value for self
:
Complex.rect(7).imag # => 0 Complex.rect(9, -4).imag # => -4
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
Returns the conjugate of self
, Complex.rect(self.imag, self.real)
:
Complex.rect(1, 2).conj # => (1-2i)
Returns self
truncated (toward zero) to a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#truncate
.
Returns true
if self
is less than 0, false
otherwise.
Returns zero.
Returns self
.
Extracts data from self
.
If block
is not given, forming objects that become the elements of a new array, and returns that array. Otherwise, yields each object.
See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Returns the count of characters (not bytes) in self
:
'foo'.length # => 3 'тест'.length # => 4 'こんにちは'.length # => 5
Contrast with String#bytesize
:
'foo'.bytesize # => 3 'тест'.bytesize # => 8 'こんにちは'.bytesize # => 15
Returns a MatchData
object (or nil
) based on self
and the given pattern
.
Note: also updates Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Computes matchdata
, which will be either a MatchData
object or nil
(see Regexp#match
):
matchdata = <tt>regexp.match(self)
With no block given, returns the computed matchdata
:
'foo'.match('f') # => #<MatchData "f"> 'foo'.match('o') # => #<MatchData "o"> 'foo'.match('x') # => nil
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match('f', 1) # => nil 'foo'.match('o', 1) # => #<MatchData "o">
With a block given, calls the block with the computed matchdata
and returns the block’s return value:
'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o"> 'foo'.match(/x/) {|matchdata| matchdata } # => nil 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
Returns true
or false
based on whether a match is found for self
and pattern
.
Note: does not update Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Returns true
if self+.match(regexp)
returns a MatchData
object, false
otherwise:
'foo'.match?(/o/) # => true 'foo'.match?('o') # => true 'foo'.match?(/x/) # => false
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match?('f', 1) # => false 'foo'.match?('o', 1) # => true
Concatenates each object in objects
to self
and returns self
:
s = 'foo' s.concat('bar', 'baz') # => "foobarbaz" s # => "foobarbaz"
For each given object object
that is an Integer
, the value is considered a codepoint and converted to a character before concatenation:
s = 'foo' s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
Related: String#<<
, which takes a single argument.
Returns a 3-element array of substrings of self
.
Matches a pattern against self
, scanning from the beginning. The pattern is:
string_or_regexp
itself, if it is a Regexp
.
Regexp.quote(string_or_regexp)
, if string_or_regexp
is a string.
If the pattern is matched, returns pre-match, first-match, post-match:
'hello'.partition('l') # => ["he", "l", "lo"] 'hello'.partition('ll') # => ["he", "ll", "o"] 'hello'.partition('h') # => ["", "h", "ello"] 'hello'.partition('o') # => ["hell", "o", ""] 'hello'.partition(/l+/) #=> ["he", "ll", "o"] 'hello'.partition('') # => ["", "", "hello"] 'тест'.partition('т') # => ["", "т", "ест"] 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
If the pattern is not matched, returns a copy of self
and two empty strings:
'hello'.partition('x') # => ["hello", "", ""]
Related: String#rpartition
, String#split
.