Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/ndr_support/integer/working_days.rb,
lib/ndr_support/integer/rounding.rb,
lib/ndr_support/integer/calculations.rb
Overview
Mixin for working_days
Instance Method Summary collapse
-
#choose(k) ⇒ Object
Gets binomial coefficients:.
- #factorial ⇒ Object
-
#round_up_to(p) ⇒ Object
Rounds up to p digits.
-
#working_days_since(date) ⇒ Object
Returns a date of the number of working days since a given date.
Instance Method Details
#choose(k) ⇒ Object
Gets binomial coefficients:
4.choose(2) #=> 6
6 7 8 9 |
# 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 ⇒ Object
11 12 13 14 |
# 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) ⇒ Object
Rounds up to p digits. For graphs. Josh Pencheon 22/08/2007
3 4 5 6 7 8 9 10 11 |
# 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) ⇒ Object
Returns a date of the number of working days since a given date
4 5 6 7 8 9 10 |
# 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 |