Sha256: ae308443726a96a87f9cdc454799ee1c6ad2e1954c59eae989e07b9249312faf

Contents?: true

Size: 1.5 KB

Versions: 21

Compression:

Stored size: 1.5 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 combinatorial(num)
      (0...num).inject(1) { |acc, i| (acc * (self - i)) / (i + 1) }
    end

    def factorial
      (1..self).inject { |acc, i| acc * i } || 0
    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

21 entries across 21 versions & 1 rubygems

Version Path
lite-ruby-1.1.4 lib/lite/ruby/integer.rb
lite-ruby-1.1.3 lib/lite/ruby/integer.rb
lite-ruby-1.1.2 lib/lite/ruby/integer.rb
lite-ruby-1.1.1 lib/lite/ruby/integer.rb
lite-ruby-1.1.0 lib/lite/ruby/integer.rb
lite-ruby-1.0.31 lib/lite/ruby/integer.rb
lite-ruby-1.0.30 lib/lite/ruby/integer.rb
lite-ruby-1.0.29 lib/lite/ruby/integer.rb
lite-ruby-1.0.28 lib/lite/ruby/integer.rb
lite-ruby-1.0.27 lib/lite/ruby/integer.rb
lite-ruby-1.0.26 lib/lite/ruby/integer.rb
lite-ruby-1.0.25 lib/lite/ruby/integer.rb
lite-ruby-1.0.24 lib/lite/ruby/integer.rb
lite-ruby-1.0.23 lib/lite/ruby/integer.rb
lite-ruby-1.0.22 lib/lite/ruby/integer.rb
lite-ruby-1.0.21 lib/lite/ruby/integer.rb
lite-ruby-1.0.20 lib/lite/ruby/integer.rb
lite-ruby-1.0.19 lib/lite/ruby/integer.rb
lite-ruby-1.0.18 lib/lite/ruby/integer.rb
lite-ruby-1.0.17 lib/lite/ruby/integer.rb