Class: Integer
- Includes:
- Style
- Defined in:
- lib/more/facets/stylize.rb,
lib/more/facets/typecast.rb,
lib/core/facets/integer/of.rb,
lib/core/facets/numeric/round.rb,
lib/core/facets/comparable/cmp.rb,
lib/core/facets/integer/bitmask.rb,
lib/core/facets/integer/factorial.rb,
lib/core/facets/integer/multiples.rb
Defined Under Namespace
Modules: Style
Class Method Summary collapse
Instance Method Summary collapse
-
#bit(bit) ⇒ Object
(also: #bit!)
Set a bit.
-
#bit?(bit) ⇒ Boolean
Is a bit set?.
-
#bitmask(mask) ⇒ Object
(also: #bitmask!)
Apply a bitmask.
-
#bitmask?(mask) ⇒ Boolean
Is bitmask set?.
-
#clear_bit(bit) ⇒ Object
Clear bit.
-
#even? ⇒ Boolean
Returns true if this integer is even, false otherwise.
-
#factorial ⇒ Object
(also: #fac)
Calculate the factorial of an integer.
-
#multiple?(number) ⇒ Boolean
Is is a multiple of a given number?.
-
#odd? ⇒ Boolean
Returns true if this integer is odd, false otherwise.
-
#of(&block) ⇒ Object
(also: #times_collect, #times_map)
Like #times but returns a collection of the yield results.
-
#round_at(*args) ⇒ Object
See Float#round_at.
-
#round_to(*args) ⇒ Object
See Float#round_to.
Methods included from Style
Class Method Details
.cast_from(object) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/more/facets/typecast.rb', line 168 def cast_from(object) return super rescue TypeCastException return object.to_i if object.respond_to? :to_i raise end |
Instance Method Details
#bit(bit) ⇒ Object Also known as: bit!
Set a bit.
0.bit!(4) #=> 8
Using an inverted bit will clear a bit.
10.bit!(~3) #=> 2
0xb0100.bit(~3) #=> 0
CREDIT: Thomas Sawyer
CREDIT: George Moschovitis
15 16 17 18 19 20 21 22 23 |
# File 'lib/core/facets/integer/bitmask.rb', line 15 def bit(bit) if bit < 0 mask = (1 << ~bit) self & ~mask else mask = (1 << bit) self | mask end end |
#bit?(bit) ⇒ Boolean
Is a bit set?
8.bit?(3) #=> true
8.bit?(2) #=> false
CREDIT: Thomas Sawyer
CREDIT: George Moschovitis
45 46 47 48 |
# File 'lib/core/facets/integer/bitmask.rb', line 45 def bit?(bit) mask = (1 << bit) (self & mask) != 0 end |
#bitmask(mask) ⇒ Object Also known as: bitmask!
Apply a bitmask.
1.bitmask(6) #=> 7
Using a inverted bitmask clears bits.
7.bitmask(~2) #=> 5
5.bitmask(~2) #=> 5
CREDIT: George Moschovitis
61 62 63 64 65 66 67 |
# File 'lib/core/facets/integer/bitmask.rb', line 61 def bitmask(mask) if mask < 0 self & mask else self | mask end end |
#bitmask?(mask) ⇒ Boolean
Is bitmask set?
7.bitmask?(7) #=> true
7.bitmask?(5) #=> true
8.bitmask?(3) #=> false
CREDIT: George Moschovitis
80 81 82 |
# File 'lib/core/facets/integer/bitmask.rb', line 80 def bitmask?(mask) (self & mask) != 0 end |
#clear_bit(bit) ⇒ Object
Clear bit.
CREDIT: George Moschovitis
32 33 34 35 |
# File 'lib/core/facets/integer/bitmask.rb', line 32 def clear_bit(bit) mask = (1 << bit) self & ~mask end |
#even? ⇒ Boolean
Returns true if this integer is even, false otherwise.
2.even? #=> true
3.even? #=> false
CREDIT: Gavin Sinclair
26 27 28 |
# File 'lib/core/facets/integer/multiples.rb', line 26 def even? self % 2 == 0 end |
#factorial ⇒ Object Also known as: fac
Calculate the factorial of an integer.
2.factorial #=> 2
3.factorial #=> 6
3.factorial #=> 24
CREDIT: Malte Milatz
11 12 13 14 15 16 |
# File 'lib/core/facets/integer/factorial.rb', line 11 def factorial return 1 if zero? f = 1 2.upto(self) { |n| f *= n } f end |
#multiple?(number) ⇒ Boolean
Is is a multiple of a given number?
7.multiple?(2) #=> false
8.multiple?(2) #=> true
CREDIT: Trans
39 40 41 |
# File 'lib/core/facets/integer/multiples.rb', line 39 def multiple?(number) self % number == 0 end |
#odd? ⇒ Boolean
Returns true if this integer is odd, false otherwise.
2.odd? #=> false
3.odd? #=> true
-99.odd? # -> true
-98.odd? # -> false
CREDIT: Gavin Sinclair
15 16 17 |
# File 'lib/core/facets/integer/multiples.rb', line 15 def odd? self % 2 == 1 end |
#of(&block) ⇒ Object Also known as: times_collect, times_map
Like #times but returns a collection of the yield results.
a = 3.of { |i| "#{i+1}" }
a => [ "1", "2", "3" ]
9 10 11 |
# File 'lib/core/facets/integer/of.rb', line 9 def of(&block) Array.new(self, &block) end |
#round_at(*args) ⇒ Object
See Float#round_at.
33 34 35 |
# File 'lib/core/facets/numeric/round.rb', line 33 def round_at(*args) to_f.round_at(*args) end |
#round_to(*args) ⇒ Object
See Float#round_to.
39 40 41 |
# File 'lib/core/facets/numeric/round.rb', line 39 def round_to(*args) to_f.round_to(*args) end |