class Integer

Constants

ROMAN_NUMERALS
SQL_BIGINT
SQL_INTEGER
SQL_SMALLINT

Public Instance Methods

bit(bit) click to toggle source
# 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) click to toggle source
# File lib/lite/ruby/integer.rb, line 25
def bit?(bit)
  mask = (1 << bit)
  (self & mask) != 0
end
bit_clear(bit) click to toggle source
# File lib/lite/ruby/integer.rb, line 30
def bit_clear(bit)
  mask = (1 << bit)
  self & ~mask
end
bitmask(mask) click to toggle source
# File lib/lite/ruby/integer.rb, line 35
def bitmask(mask)
  if mask.negative?
    self & mask
  else
    self | mask
  end
end
bitmask?(mask) click to toggle source
# File lib/lite/ruby/integer.rb, line 43
def bitmask?(mask)
  (self & mask) != 0
end
combinatorial(num) click to toggle source
# File lib/lite/ruby/integer.rb, line 47
def combinatorial(num)
  (0...num).inject(1) { |acc, i| (acc * (self - i)) / (i + 1) }
end
factorial() click to toggle source

rubocop:disable Lint/AmbiguousRange

# File lib/lite/ruby/integer.rb, line 52
def factorial
  (1..self).inject { |acc, i| acc * i } || 0
end
factors() click to toggle source

rubocop:enable Lint/AmbiguousRange

# File lib/lite/ruby/integer.rb, line 57
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) click to toggle source
# File lib/lite/ruby/integer.rb, line 69
def of(&block)
  Array.new(self, &block)
end
roman_numeral() click to toggle source
# File lib/lite/ruby/integer.rb, line 73
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
to_t()
Alias for: to_time
to_time() click to toggle source
# File lib/lite/ruby/integer.rb, line 80
def to_time
  Time.at(self)
end
Also aliased as: to_t