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, George Moschovitis
# File lib/core/facets/bitmask.rb, line 14
14: def bit(bit)
15: if bit < 0
16: mask = (1 << ~bit)
17: self & ~mask
18: else
19: mask = (1 << bit)
20: self | mask
21: end
22: end
Is a bit set?
8.bit?(3) #=> true 8.bit?(2) #=> false
CREDIT: Thomas Sawyer, George Moschovitis
# File lib/core/facets/bitmask.rb, line 43
43: def bit?(bit)
44: mask = (1 << bit)
45: (self & mask) != 0
46: end
Apply a bitmask.
1.bitmask(6) #=> 7
Using a inverted bitmask clears bits.
7.bitmask(~2) #=> 5 5.bitmask(~2) #=> 5
CREDIT: George Moschovitis
# File lib/core/facets/bitmask.rb, line 59
59: def bitmask(mask)
60: if mask < 0
61: self & mask
62: else
63: self | mask
64: end
65: end
Is bitmask set?
7.bitmask?(7) #=> true 7.bitmask?(5) #=> true 8.bitmask?(3) #=> false
CREDIT: George Moschovitis
# File lib/core/facets/bitmask.rb, line 78
78: def bitmask?(mask)
79: (self & mask) != 0
80: end
Clear bit.
CREDIT: George Moschovitis
# File lib/core/facets/bitmask.rb, line 31
31: def clear_bit(bit)
32: mask = (1 << bit)
33: self & ~mask
34: end
Returns true if this integer is even, false otherwise.
2.even? #=> true 3.even? #=> false
CREDIT: Daniel Schierbeck
# File lib/core/facets/integer/odd.rb, line 31
31: def even?
32: #self % 2 == 0
33: self & 1 == 0
34: end
Calculate the factorial of an integer.
2.factorial #=> 2 3.factorial #=> 6 3.factorial #=> 24
CREDIT: Malte Milatz
# File lib/core/facets/integer/factorial.rb, line 11
11: def factorial
12: return 1 if zero?
13: f = 1
14: 2.upto(self) { |n| f *= n }
15: f
16: end
Is self a multiple of a given number?
7.multiple?(2) #=> false 8.multiple?(2) #=> true
CREDIT: Trans
# File lib/core/facets/integer/multiple.rb, line 10
10: def multiple?(number)
11: if number.zero?
12: zero? ? true : false
13: else
14: self % number == 0
15: end
16: end
Returns true if this integer is odd, false otherwise.
2.odd? #=> false 3.odd? #=> true -99.odd? # -> true -98.odd? # -> false
CREDIT: Daniel Schierbeck
# File lib/core/facets/integer/odd.rb, line 15
15: def odd?
16: #self % 2 == 1
17: self & 1 == 1
18: end
Like # but returns a collection of the yield results.
a = 3.of { |i| "#{i+1}" }
a => [ "1", "2", "3" ]
# File lib/core/facets/integer/of.rb, line 9
9: def of(&block)
10: Array.new(self, &block)
11: end
# File lib/core/facets/integer/ordinal.rb, line 3
3: def ordinal
4: if [11,12,13].include?(self % 100)
5: "#{self}th"
6: else
7: case (self % 10)
8: when 1
9: "#{self}st"
10: when 2
11: "#{self}nd"
12: when 3
13: "#{self}rd"
14: else
15: "#{self}th"
16: end
17: end
18: end
Converts this integer to a roman numeral.
# File lib/more/facets/roman.rb, line 24
24: def roman
25: int = self
26: #return nil if integer > ROMAN_MAX
27: return "-#{(-int).roman}" if int < 0
28: return "" if int == 0
29: ROMAN_VALUES.each do |(i, v)|
30: return(i + (int-v).roman) if v <= int
31: end
32: end
See Float#round_at.
# File lib/core/facets/numeric/round.rb, line 27
27: def round_at(*args)
28: to_f.round_at(*args)
29: end
See Float#round_to.
# File lib/core/facets/numeric/round.rb, line 33
33: def round_to(*args)
34: to_f.round_to(*args)
35: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.