class Integer

Mixin for working_days

Public Instance Methods

choose(k) click to toggle source

Gets binomial coefficients:

4.choose(2) #=> 6
# File lib/ndr_support/integer/calculations.rb, line 6
def choose(k)
  fail(ArgumentError, "cannot choose #{k} from #{self}") unless (0..self) === k
  self.factorial / (k.factorial * (self - k).factorial)
end
factorial() click to toggle source
# File lib/ndr_support/integer/calculations.rb, line 11
def factorial
  fail("cannot calculate #{self}.factorial") unless self >= 0 # limited implementation
  self.zero? ? 1 : (1..self).inject { |product, i| product * i }
end
round_up_to(p) click to toggle source

Rounds up to p digits. For graphs. Josh Pencheon 22/08/2007

# File lib/ndr_support/integer/rounding.rb, line 3
def round_up_to(p)
  return nil if p > self.to_s.length || p < 0
  p = p.to_i
  s = self.to_s.split('')
  d = s[0..(p - 1)]
  d[p - 1] = s[p - 1].to_i + 1
  s[p..-1].each_with_index { |_v, i| d[i + p] = '0' }
  d.join.to_i
end
working_days_since(date) click to toggle source

Returns a date of the number of working days since a given date

# File lib/ndr_support/integer/working_days.rb, line 4
def working_days_since(date)
  times do
    date = date.next
    date = date.next while date.public_holiday? || !date.weekday?
  end
  date
end