Chautauqua Logo

Ruby class Numeric interactive

Prompt icon → Click to get an interactive sample (type in code)

Methods

Querying

finite?1.finite?; (1.0/0).finite? # true;false Documentation Interactive samplePlayground
false
infinite?1.infinite?; (1.0/0).infinite? # nil;1 Documentation Interactive samplePlayground
-1
integer?1.integer?; 1.0.integer? # true;false Documentation Interactive samplePlayground
true
positive?1.positive?; -1.positive? # true;false Documentation Interactive samplePlayground
true
negative?-1.negative?; 1.negative? # true;false Documentation Interactive samplePlayground
true
zero?0.zero?; 7.zero? # true;false Documentation Interactive samplePlayground
true
nonzero?7.nonzero?; 0.nonzero? # 7;nil Documentation Interactive samplePlayground
7
real?1.real?; Complex::I.real? # true;false Documentation Interactive samplePlayground
true

Comparing

<=>1<=>2; 1<=>1; 2<=>1 # -1;0;1 Documentation Interactive samplePlayground
-1
eql?1.eql?(1);1.eql?(1.0) # true;false;false Documentation Interactive samplePlayground
true

Converting

% (alias modulo)11 % 3; 11.modulo(3) # 2 Documentation Interactive samplePlayground
2
@ (self)n=99; n; -n # 99;-99 Documentation Interactive samplePlayground
-99
abs (alias magnitude)-12.36.abs # 1236 Documentation Interactive samplePlayground
12.36
abs2-3.abs2 # 9 Documentation Interactive samplePlayground
9
arg (alias angle phase)Complex(-1, 0).arg # 3.14159 Documentation Interactive samplePlayground
3.141592653589793
round12.36.round(1); 1236.round(-1) # 12.4;1240 Documentation Interactive samplePlayground
12.4
ceil12.36.ceil(1); 1236.ceil(-1) # 12.4;1240 Documentation Interactive samplePlayground
12.4
floor12.36.floor(1); 1236.floor(-1) # 12.3;1230 Documentation Interactive samplePlayground
12.3
truncate12.36.truncate(1); 1236.truncate(-1) # 12.3;1230 Documentation Interactive samplePlayground
12.3
coerce1.coerce(2); 1.coerce(2.0) # [2,1];[2.0,1.0] Documentation Interactive samplePlayground
[2.0, 1.0]
conj (alias conjugate)Complex(3,4).conjugate # (3-4i) Documentation Interactive samplePlayground
(5+0i)
numeratorr=Rational(6,8); r.numerator # (3/4);3 Documentation Interactive samplePlayground
(3/4); 3
denominatorr=Rational(6,8); r.denominator # (3/4);4 Documentation Interactive samplePlayground
(3/4); 3; 4
div7.div(2) # 3 Documentation Interactive samplePlayground
3
fdiv7.fdiv(2) # 3.5 Documentation Interactive samplePlayground
3.5
divmod11.divmod(3) # [3,2] Documentation Interactive samplePlayground
[3, 2]
i 'complex'2.i; (1/4.0).i # (0+2i);(0+0.25i) Documentation Interactive samplePlayground
(0+2.0i)
realComplex(3,4).real # 3 Documentation Interactive samplePlayground
3
imaginary (alias imag)Complex(3,4).imaginary; # 4 Documentation Interactive samplePlayground
4
polarComplex(-1,0).polar # [1,3.14159] Documentation Interactive samplePlayground
[1.4142135623730951, 0.7853981633974483]
quo1.quo(2); 1.0.quo(2) # (1/2);0.5 Documentation Interactive samplePlayground
(1/2)
rectangular (alias rect)Complex(3,4).rectangular # [3,4] Documentation Interactive samplePlayground
[3, 4]
remainder11.remainder(4); 11.0.remainder(4) # 3;3.0 Documentation Interactive samplePlayground
3.0
to_c(1/4.0).to_c # (0.25+0i) Documentation Interactive samplePlayground
(0.25+0i)
to_int(9/4.0).to_int # 2 Documentation Interactive samplePlayground
2

Other

Integer, Float, Rational, Complex are always frozen (immutable value types)
clonec1=1; c2=c1.clone; c2.frozen? # true Documentation
dup (alias +@)d1=1; d2=d1.dup; d2=+d1; d2.frozen? # true Documentation
step4.step(by: 2, to: 8) {|i| p i } # 4;6;8 Documentation