Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/ndr_support/integer/julian_date_conversions.rb,
lib/ndr_support/integer/rounding.rb,
lib/ndr_support/integer/calculations.rb
Overview
Extend Integer for use in our Daterange class
Instance Method Summary collapse
-
#choose(k) ⇒ Object
Gets binomial coefficients:.
- #factorial ⇒ Object
-
#jd_to_date ⇒ Object
Julian date number to Ruby Date.
- #jd_to_datetime ⇒ Object
-
#round_up_to(p) ⇒ Object
Rounds up to p digits.
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 |
#jd_to_date ⇒ Object
Julian date number to Ruby Date
6 7 8 |
# File 'lib/ndr_support/integer/julian_date_conversions.rb', line 6 def jd_to_date Date.jd(self) end |
#jd_to_datetime ⇒ Object
10 11 12 13 |
# File 'lib/ndr_support/integer/julian_date_conversions.rb', line 10 def jd_to_datetime date = jd_to_date Ourdate.build_datetime(date.year, date.month, date.day) 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 |