Sha256: 20c29497875d4628284e9f825297459aef6b6cf9ae03507be8df07e9f7085daa

Contents?: true

Size: 879 Bytes

Versions: 3

Compression:

Stored size: 879 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2017
# License: GNU APGLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

class Numeric
  def round_into num
    ((self / num) / num + 1) * num
  end

  def round_to num
    (self / num.to_f).round * num
  end

  def multiple_of?(number)
    number != 0 ? self % number == 0 : zero?
  end

  DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/

  def delimit delimiter="_"
    left, right = self.to_s.split(".")
    left.gsub!(DELIMITER_REGEX) do |digit_to_delimit|
      "#{digit_to_delimit}#{delimiter}"
    end

    [left, right].compact.join(".")
  end

  def prime?
    return false if self < 2

    not (2..(Math.sqrt(self))).any?{|f| self.multiple_of? f}
  end

  def factorial
    Math.factorial self
  end

  def factors
    (1..self).select{|f| self.multiple_of? f}
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
muflax-0.7.0 lib/muflax/num.rb
muflax-0.6.1 lib/muflax/num.rb
muflax-0.6.0 lib/muflax/num.rb