Sha256: e106985399f37cf3a6bf633671ed81e9304e7b043cad599f938a6991c1be6ff0

Contents?: true

Size: 871 Bytes

Versions: 1

Compression:

Stored size: 871 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Steffi Dorn <mail@muflax.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

1 entries across 1 versions & 1 rubygems

Version Path
muflax-0.3.14 lib/muflax/num.rb