Class: Numeric

Inherits:
Object show all
Defined in:
lib/muflax/num.rb,
lib/muflax/time.rb,
lib/muflax/blank.rb,
lib/muflax/deep_dup.rb

Overview

Copyright Freya Dorn <[email protected]>, 2017 License: GNU APGLv3 (or later) <www.gnu.org/copyleft/gpl.html>

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#blank?Boolean



16
# File 'lib/muflax/blank.rb', line 16

def blank?  ; false  ; end

#daysObject Also known as: day



11
# File 'lib/muflax/time.rb', line 11

def days          ; self.hours   * 24      ; end

#delimit(delimiter = "_") ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/muflax/num.rb', line 21

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

#factorialObject



36
37
38
# File 'lib/muflax/num.rb', line 36

def factorial
  Math.factorial self
end

#factorsObject



40
41
42
# File 'lib/muflax/num.rb', line 40

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

#fortnightsObject Also known as: fortnight



13
# File 'lib/muflax/time.rb', line 13

def fortnights    ; self.weeks   * 2       ; end

#hoursObject Also known as: hour



10
# File 'lib/muflax/time.rb', line 10

def hours         ; self.minutes * 60      ; end

#millisecondsObject Also known as: millisecond



7
# File 'lib/muflax/time.rb', line 7

def milliseconds  ; self.seconds / 1000.0  ; end

#minutesObject Also known as: minute



9
# File 'lib/muflax/time.rb', line 9

def minutes       ; self.seconds * 60      ; end

#monthsObject Also known as: month



14
# File 'lib/muflax/time.rb', line 14

def months        ; self.days    * 30      ; end

#multiple_of?(number) ⇒ Boolean



15
16
17
# File 'lib/muflax/num.rb', line 15

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

#prime?Boolean



30
31
32
33
34
# File 'lib/muflax/num.rb', line 30

def prime?
  return false if self < 2

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

#round_into(num) ⇒ Object



7
8
9
# File 'lib/muflax/num.rb', line 7

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

#round_to(num) ⇒ Object



11
12
13
# File 'lib/muflax/num.rb', line 11

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

#secondsObject Also known as: second



8
# File 'lib/muflax/time.rb', line 8

def seconds       ; self                   ; end

#weeksObject Also known as: week



12
# File 'lib/muflax/time.rb', line 12

def weeks         ; self.days    * 7       ; end

#yearsObject Also known as: year



15
# File 'lib/muflax/time.rb', line 15

def years         ; self.days    * 365     ; end