Returns a new array containing the elements of self
, sorted.
With no block given, compares elements using operator #<=>
(see Object#<=>
):
[0, 2, 3, 1].sort # => [0, 1, 2, 3]
With a block given, calls the block with each combination of pairs of elements from self
; for each pair a
and b
, the block should return a numeric:
Negative when b
is to follow a
.
Zero when a
and b
are equivalent.
Positive when a
is to follow b
.
Example:
a = [3, 2, 0, 1] a.sort {|a, b| a <=> b } # => [0, 1, 2, 3] a.sort {|a, b| b <=> a } # => [3, 2, 1, 0]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable.
Related: see Methods for Fetching.
Like Array#sort
, but returns self
with its elements sorted in place.
Related: see Methods for Assigning.
With a block given, returns a new array whose elements are all those from self
for which the block returns false
or nil
:
a = [:foo, 'bar', 2, 'bat'] a1 = a.reject {|element| element.to_s.start_with?('b') } a1 # => [:foo, 2]
With no block given, returns a new Enumerator
.
Related: Methods for Fetching.
With a block given, calls the block with each element of self
; removes each element for which the block returns a truthy value.
Returns self
if any elements removed:
a = [:foo, 'bar', 2, 'bat'] a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
Returns nil
if no elements removed.
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
Replaces the elements of self
with the elements of other_array
, which must be an array-convertible object; returns self
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.replace(['d', 'e']) # => ["d", "e"]
Related: see Methods for Assigning.
Freezes self
(if not already frozen); returns self
:
a = [] a.frozen? # => false a.freeze a.frozen? # => true
No further changes may be made to self
; raises FrozenError
if a change is attempted.
Related: Kernel#frozen?
.
Returns the new string formed by calling method #inspect
on each array element:
a = [:foo, 'bar', 2] a.inspect # => "[:foo, \"bar\", 2]"
Related: see Methods for Converting.
Prepends the given objects
to self
:
a = [:foo, 'bar', 2] a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
Related: Array#shift
; see also Methods for Assigning.
Returns elements from self
, or nil
; does not modify self
.
With no argument given, returns the first element (if available):
a = [:foo, 'bar', 2] a.first # => :foo a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
[].first # => nil
With a non-negative integer argument count
given, returns the first count
elements (as available) in a new array:
a.first(0) # => [] a.first(2) # => [:foo, "bar"] a.first(50) # => [:foo, "bar", 2]
Related: see Methods for Querying.
Returns elements from self
, or nil
; self
is not modified.
With no argument given, returns the last element, or nil
if self
is empty:
a = [:foo, 'bar', 2] a.last # => 2 a # => [:foo, "bar", 2] [].last # => nil
With non-negative integer argument count
given, returns a new array containing the trailing count
elements of self
, as available:
a = [:foo, 'bar', 2] a.last(2) # => ["bar", 2] a.last(50) # => [:foo, "bar", 2] a.last(0) # => [] [].last(3) # => []
Related: see Methods for Fetching.
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 string containing the place-value representation of self
in radix base
(in 2..36).
12345.to_s # => "12345" 12345.to_s(2) # => "11000000111001" 12345.to_s(8) # => "30071" 12345.to_s(10) # => "12345" 12345.to_s(16) # => "3039" 12345.to_s(36) # => "9ix" 78546939656932.to_s(36) # => "rubyrules"
Raises an exception if base
is out of range.
Calls the given block with each integer value from self
up to limit
; returns self
:
a = [] 5.upto(10) {|i| a << i } # => 5 a # => [5, 6, 7, 8, 9, 10] a = [] -5.upto(0) {|i| a << i } # => -5 a # => [-5, -4, -3, -2, -1, 0] 5.upto(4) {|i| fail 'Cannot happen' } # => 5
With no block given, returns an Enumerator
.
Calls the given block with each integer value from self
down to limit
; returns self
:
a = [] 10.downto(5) {|i| a << i } # => 10 a # => [10, 9, 8, 7, 6, 5] a = [] 0.downto(-5) {|i| a << i } # => 0 a # => [0, -1, -2, -3, -4, -5] 4.downto(5) {|i| fail 'Cannot happen' } # => 4
With no block given, returns an Enumerator
.
Returns the predecessor of self
(equivalent to self - 1
):
1.pred #=> 0 -1.pred #=> -2
Related: Integer#succ
(successor value).
Converts self
to a Float:
1.to_f # => 1.0 -1.to_f # => -1.0
If the value of self
does not fit in a Float
, the result is infinity:
(10**400).to_f # => Infinity (-10**400).to_f # => -Infinity
Returns an integer that is a “floor” value for self
, as specified by the given ndigits
, which must be an integer-convertible object.
When self
is zero, returns zero (regardless of the value of ndigits
):
0.floor(2) # => 0 0.floor(-2) # => 0
When self
is non-zero and ndigits
is non-negative, returns self
:
555.floor # => 555 555.floor(50) # => 555
When self
is non-zero and ndigits
is negative, returns a value based on a computed granularity:
The granularity is 10 ** ndigits.abs
.
The returned value is the largest multiple of the granularity that is less than or equal to self
.
Examples with positive self
:
ndigits | Granularity | 1234.floor(ndigits) |
---|---|---|
-1 | 10 | 1230 |
-2 | 100 | 1200 |
-3 | 1000 | 1000 |
-4 | 10000 | 0 |
-5 | 100000 | 0 |
Examples with negative self
:
ndigits | Granularity | -1234.floor(ndigits) |
---|---|---|
-1 | 10 | -1240 |
-2 | 100 | -1300 |
-3 | 1000 | -2000 |
-4 | 10000 | -10000 |
-5 | 100000 | -100000 |
Related: Integer#ceil
.
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
; intended for compatibility to character literals in Ruby
1.9.
Returns self
(which is already an Integer).
Returns the value as a rational.
1.to_r #=> (1/1) (1<<64).to_r #=> (18446744073709551616/1)
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric
, or an instance of one of its subclasses: Complex, Float
, Integer
, Rational
; see Rectangular Coordinates:
Complex.rect(3) # => (3+0i) Complex.rect(3, Math::PI) # => (3+3.141592653589793i) Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
Complex.rectangular is an alias for Complex.rect.
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric
, or an instance of one of its subclasses: Complex, Float
, Integer
, Rational
; see Rectangular Coordinates:
Complex.rect(3) # => (3+0i) Complex.rect(3, Math::PI) # => (3+3.141592653589793i) Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
Complex.rectangular is an alias for Complex.rect.
Returns the real value for self
:
Complex.rect(7).real # => 7 Complex.rect(9, -4).real # => 9
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).real # => 0.7071067811865476 # Square root of 2.
Returns the array [self.real, self.imag]
:
Complex.rect(1, 2).rect # => [1, 2]
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1.0, 1.0).rect # => [0.5403023058681398, 0.8414709848078965]
Complex#rectangular
is an alias for Complex#rect
.