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
.
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 false
; for compatibility with Numeric#real?
.
Returns a string representation of self
:
Complex.rect(2).to_s # => "2+0i" Complex.rect(-8, 6).to_s # => "-8+6i" Complex.rect(0, Rational(1, 2)).to_s # => "0+1/2i" Complex.rect(0, Float::INFINITY).to_s # => "0+Infinity*i" Complex.rect(Float::NAN, Float::NAN).to_s # => "NaN+NaN*i"
Returns the value of self.real
as an Integer
, if possible:
Complex.rect(1, 0).to_i # => 1 Complex.rect(1, Rational(0, 1)).to_i # => 1
Raises RangeError
if self.imag
is not exactly zero (either Integer(0)
or Rational(0, n)
).
Returns the value of self.real
as a Float
, if possible:
Complex.rect(1, 0).to_f # => 1.0 Complex.rect(1, Rational(0, 1)).to_f # => 1.0
Raises RangeError
if self.imag
is not exactly zero (either Integer(0)
or Rational(0, n)
).
Returns the value of self.real
as a Rational
, if possible:
Complex.rect(1, 0).to_r # => (1/1) Complex.rect(1, Rational(0, 1)).to_r # => (1/1) Complex.rect(1, 0.0).to_r # => (1/1)
Raises RangeError
if self.imag
is not exactly zero (either Integer(0)
or Rational(0, n)
) and self.imag.to_r
is not exactly zero.
Related: Complex#rationalize
.
Returns self
.