Calls the block, if given, with each element of self
; returns a new Array
whose elements are the return values from the block:
a = [:foo, 'bar', 2] a1 = a.map {|element| element.class } a1 # => [Symbol, String, Integer]
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a1 = a.map a1 # => #<Enumerator: [:foo, "bar", 2]:map>
Calls the block, if given, with each element; replaces the element with the block’s return value:
a = [:foo, 'bar', 2] a.map! { |element| element.class } # => [Symbol, String, Integer]
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a1 = a.map! a1 # => #<Enumerator: [:foo, "bar", 2]:map!>
Returns one of the following:
The maximum-valued element from self
.
A new Array
of maximum-valued elements selected from self
.
When no block is given, each element in self
must respond to method <=>
with an Integer
.
With no argument and no block, returns the element in self
having the maximum value per method <=>
:
[0, 1, 2].max # => 2
With an argument Integer
n
and no block, returns a new Array
with at most n
elements, in descending order per method <=>
:
[0, 1, 2, 3].max(3) # => [3, 2, 1] [0, 1, 2, 3].max(6) # => [3, 2, 1, 0]
When a block is given, the block must return an Integer
.
With a block and no argument, calls the block self.size-1
times to compare elements; returns the element having the maximum value per the block:
['0', '00', '000'].max {|a, b| a.size <=> b.size } # => "000"
With an argument n
and a block, returns a new Array
with at most n
elements, in descending order per the block:
['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
Returns a new Array
that is a recursive flattening of self
:
Each non-Array element is unchanged.
Each Array
is replaced by its individual elements.
With non-negative Integer
argument level
, flattens recursively through level
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(0) # => [0, [1, [2, 3], 4], 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(1) # => [0, 1, [2, 3], 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(2) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(3) # => [0, 1, 2, 3, 4, 5]
With no argument, a nil
argument, or with negative argument level
, flattens all levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten # => [0, 1, 2] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(-1) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(-2) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten(-1) # => [0, 1, 2]
Replaces each nested Array
in self
with the elements from that Array
; returns self
if any changes, nil
otherwise.
With non-negative Integer
argument level
, flattens recursively through level
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(1) # => [0, 1, [2, 3], 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(2) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(3) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten!(1) # => nil
With no argument, a nil
argument, or with negative argument level
, flattens all levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten! # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten! # => nil a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(-1) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(-2) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten!(-1) # => nil
When invoked with a block, yield all permutations of elements of self
; returns self
. The order of permutations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple permutations of self
.
Example:
a = [0, 1, 2] a.permutation(2) {|permutation| p permutation }
Output:
[0, 1] [0, 2] [1, 0] [1, 2] [2, 0] [2, 1]
Another example:
a = [0, 1, 2] a.permutation(3) {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
When n
is zero, calls the block once with a new empty Array
:
a = [0, 1, 2] a.permutation(0) {|permutation| p permutation }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.permutation(-1) {|permutation| fail 'Cannot happen' } a.permutation(4) {|permutation| fail 'Cannot happen' }
When a block given but no argument, behaves the same as a.permutation(a.size)
:
a = [0, 1, 2] a.permutation {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.permutation # => #<Enumerator: [0, 1, 2]:permutation> a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
Calls the block, if given, with combinations of elements of self
; returns self
. The order of combinations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple combinations of self
.
Example:
a = [0, 1, 2] a.combination(2) {|combination| p combination }
Output:
[0, 1] [0, 2] [1, 2]
Another example:
a = [0, 1, 2] a.combination(3) {|combination| p combination }
Output:
[0, 1, 2]
When n
is zero, calls the block once with a new empty Array
:
a = [0, 1, 2] a1 = a.combination(0) {|combination| p combination }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.combination(-1) {|combination| fail 'Cannot happen' } a.combination(4) {|combination| fail 'Cannot happen' }
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
Iterates over array elements.
When a block given, passes each successive array element to the block; returns self
:
a = [:foo, 'bar', 2] a.each {|element| puts "#{element.class} #{element}" }
Output:
Symbol foo String bar Integer 2
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.each {|element| puts element; a.clear if element.to_s.start_with?('b') }
Output:
foo bar
When no block given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.each e # => #<Enumerator: [:foo, "bar", 2]:each> a1 = e.each {|element| puts "#{element.class} #{element}" }
Output:
Symbol foo String bar Integer 2
Related: each_index
, reverse_each
.
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 a 1-character string containing the character represented by the value of self
, according to the given encoding
.
65.chr # => "A" 0.chr # => "\x00" 255.chr # => "\xFF" string = 255.chr(Encoding::UTF_8) string.encoding # => Encoding::UTF_8
Raises an exception if self
is negative.
Related: Integer#ord
.
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
.
Returns the remainder after dividing self
by other
.
Examples:
11.remainder(4) # => 3 11.remainder(-4) # => 3 -11.remainder(4) # => -3 -11.remainder(-4) # => -3 12.remainder(4) # => 0 12.remainder(-4) # => 0 -12.remainder(4) # => 0 -12.remainder(-4) # => 0 13.remainder(4.0) # => 1.0 13.remainder(Rational(4, 1)) # => (1/1)
Returns self
.
Returns 1
.
Returns the value as a rational. The optional argument eps
is always ignored.
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 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 absolute value (magnitude) for self
; see polar coordinates:
Complex.polar(-1, 0).abs # => 1.0
If self
was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.
Returns the conjugate of self
, Complex.rect(self.imag, self.real)
:
Complex.rect(1, 2).conj # => (1-2i)
Returns the Complex object created from the numerators of the real and imaginary parts of self
, after converting each part to the lowest common denominator of the two:
c = Complex.rect(Rational(2, 3), Rational(3, 4)) # => ((2/3)+(3/4)*i) c.numerator # => (8+9i)
In this example, the lowest common denominator of the two parts is 12; the two converted parts may be thought of as Rational(8, 12) and Rational(9, 12), whose numerators, respectively, are 8 and 9; so the returned value of c.numerator
is Complex.rect(8, 9)
.
Related: Complex#denominator
.
Returns the denominator of self
, which is the least common multiple of self.real.denominator
and self.imag.denominator
:
Complex.rect(Rational(1, 2), Rational(2, 3)).denominator # => 6
Note that n.denominator
of a non-rational numeric is 1
.
Related: Complex#numerator
.
Returns a Rational
object whose value is exactly or approximately equivalent to that of self.real
.
With no argument epsilon
given, returns a Rational object whose value is exactly equal to that of self.real.rationalize
:
Complex.rect(1, 0).rationalize # => (1/1) Complex.rect(1, Rational(0, 1)).rationalize # => (1/1) Complex.rect(3.14159, 0).rationalize # => (314159/100000)
With argument epsilon
given, returns a Rational object whose value is exactly or approximately equal to that of self.real
to the given precision:
Complex.rect(3.14159, 0).rationalize(0.1) # => (16/5) Complex.rect(3.14159, 0).rationalize(0.01) # => (22/7) Complex.rect(3.14159, 0).rationalize(0.001) # => (201/64) Complex.rect(3.14159, 0).rationalize(0.0001) # => (333/106) Complex.rect(3.14159, 0).rationalize(0.00001) # => (355/113) Complex.rect(3.14159, 0).rationalize(0.000001) # => (7433/2366) Complex.rect(3.14159, 0).rationalize(0.0000001) # => (9208/2931) Complex.rect(3.14159, 0).rationalize(0.00000001) # => (47460/15107) Complex.rect(3.14159, 0).rationalize(0.000000001) # => (76149/24239) Complex.rect(3.14159, 0).rationalize(0.0000000001) # => (314159/100000) Complex.rect(3.14159, 0).rationalize(0.0) # => (3537115888337719/1125899906842624)
Related: Complex#to_r
.
Returns zero as a Rational:
nil.rationalize # => (0/1)
Argument eps
is ignored.