Class: Integer
Constant Summary collapse
- ROMAN_NUMERALS =
YAML.load_file( File.('formats/integer_roman_numerals.yml', File.dirname(__FILE__)) ).freeze
- SQL_SMALLINT =
32_767- SQL_INTEGER =
2_147_483_647- SQL_BIGINT =
9_223_372_036_854_775_807
Instance Method Summary collapse
- #bit(bit) ⇒ Object
- #bit?(bit) ⇒ Boolean
- #bit_clear(bit) ⇒ Object
- #bitmask(mask) ⇒ Object
- #bitmask?(mask) ⇒ Boolean
- #combinatorial(num) ⇒ Object
- #factorial ⇒ Object
- #factors ⇒ Object
- #of(&block) ⇒ Object
- #roman_numeral ⇒ Object
- #to_time ⇒ Object (also: #to_t)
Instance Method Details
#bit(bit) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/lite/ruby/integer.rb', line 15 def bit(bit) if bit.negative? mask = (1 << ~bit) self & ~mask else mask = (1 << bit) self | mask end end |
#bit?(bit) ⇒ Boolean
25 26 27 28 |
# File 'lib/lite/ruby/integer.rb', line 25 def bit?(bit) mask = (1 << bit) (self & mask) != 0 end |
#bit_clear(bit) ⇒ Object
30 31 32 33 |
# File 'lib/lite/ruby/integer.rb', line 30 def bit_clear(bit) mask = (1 << bit) self & ~mask end |
#bitmask(mask) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/lite/ruby/integer.rb', line 35 def bitmask(mask) if mask.negative? self & mask else self | mask end end |
#bitmask?(mask) ⇒ Boolean
43 44 45 |
# File 'lib/lite/ruby/integer.rb', line 43 def bitmask?(mask) (self & mask) != 0 end |
#combinatorial(num) ⇒ Object
47 48 49 |
# File 'lib/lite/ruby/integer.rb', line 47 def combinatorial(num) (0...num).inject(1) { |acc, i| (acc * (self - i)) / (i + 1) } end |
#factorial ⇒ Object
51 52 53 |
# File 'lib/lite/ruby/integer.rb', line 51 def factorial (1..self).inject { |acc, i| acc * i } || 0 end |
#factors ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lite/ruby/integer.rb', line 55 def factors limit = Math.sqrt(self).floor (1..limit).each_with_object([]) do |i, array| next unless (self % i).zero? sq = (self / i) array.push(i) array.push(sq) if sq != i end end |
#of(&block) ⇒ Object
67 68 69 |
# File 'lib/lite/ruby/integer.rb', line 67 def of(&block) Array.new(self, &block) end |
#roman_numeral ⇒ Object
71 72 73 74 75 76 |
# File 'lib/lite/ruby/integer.rb', line 71 def roman_numeral return '' if zero? return "-#{(-self).roman_numeral}" if negative? ROMAN_NUMERALS.each { |key, val| break "#{key}#{(self - val).roman_numeral}" if val <= self } end |