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

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

Converting

Integer.sqrtInteger.sqrt(9) # 3 Documentation Interactive samplePlayground
3
Integer.try_convertInteger.try_convert(1.25) # 1 Documentation Interactive samplePlayground
1
% (alias modulo)11 % 3; 11.modulo(3) # 2 Documentation Interactive samplePlayground
2
&format('%04b',0b1010 & 0b1100) # 8;"0100" Documentation Interactive samplePlayground
0100
*2 * 3 # 6 Documentation Interactive samplePlayground
6
** (also pow)3 ** 2 # 9 Documentation Interactive samplePlayground
9
pow3.pow(2) # 9 Documentation Interactive samplePlayground
9
+3 + 2 # 5 Documentation Interactive samplePlayground
5
-3 - 2 # 1 Documentation Interactive samplePlayground
1
/8 / 2 # 2 Documentation Interactive samplePlayground
4
<<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
^ (bitwise)"%04b" % (0b0101 ^ 0b0110) # 0011 Documentation Interactive samplePlayground
0011
| (bitwise)"%04b" % (0b0101 | 0b0110) # 0111 Documentation Interactive samplePlayground
0111
round(d)1236.round(-1);-1236.round(-1) # 1240;-1240 Documentation Interactive samplePlayground
1230
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
chr42.chr; 255.chr # *; \x00 Documentation Interactive samplePlayground
"*"
digits123.digits;0.d…;-1.d… # [3,2,1];[0];…Error Documentation Interactive samplePlayground
[3, 2, 1]
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;1.33;-4.5 Documentation Interactive samplePlayground
3
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) # 3;-3 Documentation Interactive samplePlayground
3
pred4.pred; -4.pred # 3;-5 Documentation Interactive samplePlayground
3
next (alias succ)4.next; -4.next # 5;-3 Documentation Interactive samplePlayground
5
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"

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]