Chautauqua Logo

Ruby class Integer interactive

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

Methods

Querying

allbits?0b1010.allbits?(0b1000) # true Documentation Interactive samplePlayground
true; false
anybits?0b1010.anybits?(0b1100) # true Documentation Interactive samplePlayground
true; false
nobits?0b1100.nobits?(0b0011) # true Documentation Interactive samplePlayground
false; true
even?4.even? # true Documentation Interactive samplePlayground
[false, true, false, true]
odd?3.odd? # true Documentation Interactive samplePlayground
[true, false, true, false]
zero?0.zero? # true Documentation Interactive samplePlayground
false
integer?3.integer? # true Documentation Interactive samplePlayground
true; false
bit_length0b1010.bit_length # 4 Documentation Interactive samplePlayground
8
size1.size # 8 (bytes,machine dependent) Documentation Interactive samplePlayground
machine dependent

Comparing

<1 < 1 # false Documentation Interactive samplePlayground
false
<=1 <= 1 # true Documentation Interactive samplePlayground
true
<=>1<=>2; 1<=>1; 2<=>1 # -1;0;1 Documentation Interactive samplePlayground
-1
== (alias ===)1 == 1 # true Documentation Interactive samplePlayground
true
>1 > 1 # false Documentation Interactive samplePlayground
false
>=1 >= 1 # true Documentation Interactive samplePlayground
true

Arithmetic

+3 + 2 # 5 Documentation Interactive samplePlayground
5
-3 - 2 # 1 Documentation Interactive samplePlayground
1
*2 * 3 # 6 Documentation Interactive samplePlayground
6
/8 / 2 # 4 Documentation Interactive samplePlayground
4
% (alias modulo)11 % 3; 11.modulo(3) # 2 Documentation Interactive samplePlayground
2
** (also pow)3 ** 2 # 9 Documentation Interactive samplePlayground
9
pow3.pow(2) # 9 Documentation Interactive samplePlayground
9
div9.div(3); 9.div(4); 9.div(-2) # 3;2;-5 Documentation Interactive samplePlayground
3
fdiv9.fdiv(3);4.fdiv(3);9.fdiv(-2) # 3.0;1.33…;-4.5 Documentation Interactive samplePlayground
3.0
divmod9.divmod(4); 5.divmod(-2) # [2,1];[-3,-1] Documentation Interactive samplePlayground
[2, 1]; [-3, -1]; [-3, 1]
remainder11.remainder(3); -11.remainder(4) # 2;-3 Documentation Interactive samplePlayground
2
ceildiv7.ceildiv(2) # 4 Documentation Interactive samplePlayground
-3
abs (alias magnitude)-5.abs # 5 Documentation Interactive samplePlayground
1236
-@-(5) # -5 Documentation Interactive samplePlayground
5
Integer.sqrtInteger.sqrt(9) # 3 Documentation Interactive samplePlayground
3
gcd12.gcd(8) # 4 Documentation Interactive samplePlayground
12
lcm12.lcm(8) # 24 Documentation Interactive samplePlayground
12
gcdlcm12.gcdlcm(8) # [4,24] Documentation Interactive samplePlayground
[12, 180]
pred4.pred; -4.pred # 3;-5 Documentation Interactive samplePlayground
3
next (alias succ)4.next; -4.next # 5;-3 Documentation Interactive samplePlayground
5

Bitwise

&format('%04b',0b1010 & 0b1100) # "1000" Documentation Interactive samplePlayground
0100
| (bitwise)"%04b" % (0b0101 | 0b0110) # 0111 Documentation Interactive samplePlayground
0111
^ (bitwise)"%04b" % (0b0101 ^ 0b0110) # 0011 Documentation Interactive samplePlayground
0011
~ (bitwise not)~5 # -6 Documentation Interactive samplePlayground
-11
<<n=0b1010; "%04b" % (n << 1) # 10100 Documentation Interactive samplePlayground
10100
>>n=0b1010; "%04b" % (n >> 1) # 0101 Documentation Interactive samplePlayground
0101
[] (bits)n=0b10; n[0];n[1];n[2];n[3] # 0;1;0;0 Documentation Interactive samplePlayground
0

Rounding

round(d)1236.round(-1);-1236.round(-1) # 1240;-1240 Documentation Interactive samplePlayground
1240
ceil(d)1236.ceil(-1);-1236.ceil(-1) # 1240;-1230 Documentation Interactive samplePlayground
1240
floor(d)1236.floor(-1);-1236.floor(-1) # 1230;-1240 Documentation Interactive samplePlayground
1230
truncate(d)1236.truncate(-1);-1236.t…(-1) # 1230;-1230 Documentation Interactive samplePlayground
1230

Converting

Integer.try_convertInteger.try_convert(1.25) # 1 Documentation Interactive samplePlayground
1
chr42.chr; 255.chr # *;\xFF Documentation Interactive samplePlayground
"*"
digits123.digits;0.d…;-1.d… # [3,2,1];[0];Math::DomainError Documentation Interactive samplePlayground
[3, 2, 1]
to_f4.to_f; -4.to_f # 4.0;-4.0 Documentation Interactive samplePlayground
4.0
to_s (alias inspect)4.to_s;-4.to_s;255.to_s(16) # "4";"-4";"ff" Documentation Interactive samplePlayground
"ff"
to_i (alias to_int)4.to_i # 4 (self) Documentation Interactive samplePlayground
4
to_r3.to_r # (3/1) Documentation Interactive samplePlayground
(3/1)
rationalize3.rationalize # (3/1) Documentation Interactive samplePlayground
(3/1)
numerator3.numerator # 3 (self) Documentation Interactive samplePlayground
3
denominator3.denominator # 1 (always) Documentation Interactive samplePlayground
1
ord97.ord # 97 (self) Documentation Interactive samplePlayground
97
coerce1.coerce(2.5) # [2.5,1.0] Documentation Interactive samplePlayground
[2, 1]; [2.5, 1.0]

Other

downtoa=[]; 10.downto(7) {|i| a<<i} # [10,9,8,7] Documentation Interactive samplePlayground
[2, 1, 0, -1, -2, -3]
timesa=[]; 3.times {|i| a.push(i)} # [0,1,2] Documentation Interactive samplePlayground
[0, 1, 2]
uptoa=[]; 5.upto(8) {|i| a << i} # [5,6,7,8] Documentation Interactive samplePlayground
[5, 6, 7, 8]