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
.
Returns self
as a Complex
object.
Returns array [self, 0]
.
Returns array [self, 0]
.
Returns the remainder after dividing self
by other
.
Of the Core and Standard Library classes, only Float
and Rational
use this implementation.
Examples:
11.0.remainder(4) # => 3.0 11.0.remainder(-4) # => 3.0 -11.0.remainder(4) # => -3.0 -11.0.remainder(-4) # => -3.0 12.0.remainder(4) # => 0.0 12.0.remainder(-4) # => 0.0 -12.0.remainder(4) # => -0.0 -12.0.remainder(-4) # => -0.0 13.0.remainder(4.0) # => 1.0 13.0.remainder(Rational(4, 1)) # => 1.0 Rational(13, 1).remainder(4) # => (1/1) Rational(13, 1).remainder(-4) # => (1/1) Rational(-13, 1).remainder(4) # => (-1/1) Rational(-13, 1).remainder(-4) # => (-1/1)
Returns the largest float or integer that is less than or equal to self
, as specified by the given ‘ndigits`, which must be an integer-convertible object.
Equivalent to self.to_f.floor(ndigits)
.
Related: ceil
, Float#floor
.
Generates a sequence of numbers; with a block given, traverses the sequence.
Of the Core and Standard Library classes, Integer
, Float
, and Rational
use this implementation.
A quick example:
squares = [] 1.step(by: 2, to: 10) {|i| squares.push(i*i) } squares # => [1, 9, 25, 49, 81]
The generated sequence:
Begins with self
.
Continues at intervals of by
(which may not be zero).
Ends with the last number that is within or equal to to
; that is, less than or equal to to
if by
is positive, greater than or equal to to
if by
is negative. If to
is nil
, the sequence is of infinite length.
If a block is given, calls the block with each number in the sequence; returns self
. If no block is given, returns an Enumerator::ArithmeticSequence
.
Keyword Arguments
With keyword arguments by
and to
, their values (or defaults) determine the step and limit:
# Both keywords given. squares = [] 4.step(by: 2, to: 10) {|i| squares.push(i*i) } # => 4 squares # => [16, 36, 64, 100] cubes = [] 3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3 cubes # => [27.0, 3.375, 0.0, -3.375, -27.0] squares = [] 1.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) } squares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0] squares = [] Rational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) } squares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0] # Only keyword to given. squares = [] 4.step(to: 10) {|i| squares.push(i*i) } # => 4 squares # => [16, 25, 36, 49, 64, 81, 100] # Only by given. # Only keyword by given squares = [] 4.step(by:2) {|i| squares.push(i*i); break if i > 10 } squares # => [16, 36, 64, 100, 144] # No block given. e = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3)) e.class # => Enumerator::ArithmeticSequence
Positional Arguments
With optional positional arguments to
and by
, their values (or defaults) determine the step and limit:
squares = [] 4.step(10, 2) {|i| squares.push(i*i) } # => 4 squares # => [16, 36, 64, 100] squares = [] 4.step(10) {|i| squares.push(i*i) } squares # => [16, 25, 36, 49, 64, 81, 100] squares = [] 4.step {|i| squares.push(i*i); break if i > 10 } # => nil squares # => [16, 25, 36, 49, 64, 81, 100, 121]
Implementation Notes
If all the arguments are integers, the loop operates using an integer counter.
If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed floor(n + n*Float::EPSILON) + 1 times, where n = (limit - self)/step.
Returns true
if self
is a real number (i.e. not Complex
).
Returns self
.
Returns self
interpreted as a Complex
object; leading whitespace and trailing garbage are ignored:
'9'.to_c # => (9+0i) '2.5'.to_c # => (2.5+0i) '2.5/1'.to_c # => ((5/2)+0i) '-3/2'.to_c # => ((-3/2)+0i) '-i'.to_c # => (0-1i) '45i'.to_c # => (0+45i) '3-4i'.to_c # => (3-4i) '-4e2-4e-2i'.to_c # => (-400.0-0.04i) '-0.0-0.0i'.to_c # => (-0.0-0.0i) '1/2+3/4i'.to_c # => ((1/2)+(3/4)*i) '1.0@0'.to_c # => (1+0.0i) "1.0@#{Math::PI/2}".to_c # => (0.0+1i) "1.0@#{Math::PI}".to_c # => (-1+0.0i)
Returns Complex zero if the string cannot be converted:
'ruby'.to_c # => (0+0i)
See Kernel#Complex
.
Returns the result of interpreting leading characters in str
as a rational. Leading whitespace and extraneous characters past the end of a valid number are ignored. Digit sequences can be separated by an underscore. If there is not a valid number at the start of str
, zero is returned. This method never raises an exception.
' 2 '.to_r #=> (2/1) '300/2'.to_r #=> (150/1) '-9.2'.to_r #=> (-46/5) '-9.2e2'.to_r #=> (-920/1) '1_234_567'.to_r #=> (1234567/1) '21 June 09'.to_r #=> (21/1) '21/06/09'.to_r #=> (7/2) 'BWV 1079'.to_r #=> (0/1)
NOTE: “0.3”.to_r isn’t the same as 0.3.to_r. The former is equivalent to “3/10”.to_r, but the latter isn’t so.
"0.3".to_r == 3/10r #=> true 0.3.to_r == 3/10r #=> false
See also Kernel#Rational
.
With a block given, calls the block with each String
value returned by successive calls to String#succ
; the first value is self
, the next is self.succ
, and so on; the sequence terminates when value other_string
is reached; returns self
:
'a8'.upto('b6') {|s| print s, ' ' } # => "a8"
Output:
a8 a9 b0 b1 b2 b3 b4 b5 b6
If argument exclusive
is given as a truthy object, the last value is omitted:
'a8'.upto('b6', true) {|s| print s, ' ' } # => "a8"
Output:
a8 a9 b0 b1 b2 b3 b4 b5
If other_string
would not be reached, does not call the block:
'25'.upto('5') {|s| fail s } 'aa'.upto('a') {|s| fail s }
With no block given, returns a new Enumerator:
'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>