Sha256: a38a18647ad340f3a4321e5deef5aee8594c621e17f97e5ca03bfd33c9911a04

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

if Lite::Ruby.configuration.monkey_patches.include?('integer')
  class Integer

    ROMAN_NUMERALS ||= {
      M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4,
      I: 1
    }.freeze

    def bit(bit)
      if bit.negative?
        mask = (1 << ~bit)
        self & ~mask
      else
        mask = (1 << bit)
        self | mask
      end
    end

    def bit?(bit)
      mask = (1 << bit)
      (self & mask) != 0
    end

    def bit_clear(bit)
      mask = (1 << bit)
      self & ~mask
    end

    def bitmask(mask)
      if mask.negative?
        self & mask
      else
        self | mask
      end
    end

    def bitmask?(mask)
      (self & mask) != 0
    end

    def factorial
      return 1 if zero?

      2.upto(self).inject(1) { |acc, i| acc * i }
    end

    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

    def of(&block)
      Array.new(self, &block)
    end

    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

    def to_time
      Time.at(self)
    end

    alias to_t to_time unless defined?(to_t)

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-ruby-1.0.15 lib/lite/ruby/integer.rb