This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
## Integer#bit
|
||||
|
||||
require 'facets/integer/bitmask'
|
||||
|
||||
0.bit(0).assert == 1
|
||||
0.bit(1).assert == 2
|
||||
0.bit(2).assert == 4
|
||||
0.bit(3).assert == 8
|
||||
|
||||
Negate
|
||||
|
||||
1.bit(~0).assert == 0
|
||||
2.bit(~1).assert == 0
|
||||
4.bit(~2).assert == 0
|
||||
8.bit(~3).assert == 0
|
||||
|
||||
## Integer#bit?
|
||||
|
||||
a = 8
|
||||
assert(! a.bit?(0))
|
||||
assert(! a.bit?(1))
|
||||
assert(! a.bit?(2))
|
||||
assert( a.bit?(3))
|
||||
assert(! a.bit?(4))
|
||||
assert(! a.bit?(5))
|
||||
|
||||
## Integer#bit_clear
|
||||
|
||||
1.bit_clear(0).assert == 0
|
||||
2.bit_clear(1).assert == 0
|
||||
4.bit_clear(2).assert == 0
|
||||
8.bit_clear(3).assert == 0
|
||||
|
||||
## Integer#bitmask
|
||||
|
||||
a = 1
|
||||
m = Bit(4)
|
||||
a = a.bitmask(m)
|
||||
a.assert == 17
|
||||
assert( a.bitmask?(m) )
|
||||
|
||||
## Kernel#Bit
|
||||
|
||||
n = Bit(4)
|
||||
n.assert == 16
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
## Integer#factorial
|
||||
|
||||
require 'facets/integer/factorial'
|
||||
|
||||
0.factorial.assert == 1
|
||||
1.factorial.assert == 1
|
||||
2.factorial.assert == 2
|
||||
3.factorial.assert == 6
|
||||
4.factorial.assert == 24
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Numeric#length
|
||||
|
||||
require 'facets/numeric/length'
|
||||
|
||||
10.length.assert == 10
|
||||
10.0.length.assert == 10
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
## Integer#multiple?
|
||||
|
||||
require 'facets/integer/multiple'
|
||||
|
||||
Is a number an even multiple of another?
|
||||
|
||||
1.multiple?(2) #=> false
|
||||
5.multiple?(3) #=> false
|
||||
|
||||
2.multiple?(2) #=> true
|
||||
6.multiple?(3) #=> true
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Integer#of
|
||||
|
||||
require 'facets/integer/of'
|
||||
|
||||
Similar to `Enumerable#map` and `Enumerable#collect` but for a number
|
||||
of times.
|
||||
|
||||
a = 4
|
||||
b = a.of{ |i| i*2 }
|
||||
b.assert == [0,2,4,6]
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Integer#ordinal
|
||||
|
||||
require 'facets/integer/ordinal'
|
||||
|
||||
Produce the ordinal name of a number.
|
||||
|
||||
1.ordinal.assert == '1st'
|
||||
2.ordinal.assert == '2nd'
|
||||
3.ordinal.assert == '3rd'
|
||||
4.ordinal.assert == '4th'
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Integer#times_collect
|
||||
|
||||
require 'facets/integer/of'
|
||||
|
||||
a = 4
|
||||
b = a.times_collect{ |i| i*2 }
|
||||
b.assert == [0,2,4,6]
|
||||
|
||||
See `Integer#of` which is an alias.
|
||||
Reference in New Issue
Block a user