Module: Hammock::NumericPatches::InstanceMethods

Defined in:
lib/hammock/monkey_patches/numeric.rb

Instance Method Summary collapse

Instance Method Details

#commasObject



23
24
25
26
27
28
29
30
# File 'lib/hammock/monkey_patches/numeric.rb', line 23

def commas
  if self < 1000
    self
  else
    whole, fract = self.to_s.split('.')
    [ whole.reverse.scan(/\d{1,3}/).join(',').reverse, fract ].squash.join('.')
  end
end

#sigfigs(num, opts = {}) ⇒ Object

Round the value to num significant figures



33
34
35
36
37
38
# File 'lib/hammock/monkey_patches/numeric.rb', line 33

def sigfigs num, opts = {}
  shift = 10 ** (num - self.floor.to_s.length) # preserve this many digits after the decimal place, ** 10 for scaling
  val = (self * shift).round * 1.0 / shift # shift decimal place, chop off remainder, shift back
  val = val.floor if opts[:drop_trailing_zeros] && (val == val.floor) # drop '.0' if requested and present
  val # done
end

#xsecsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hammock/monkey_patches/numeric.rb', line 40

def xsecs
  value = self.abs
  past = (self < 0)

  if value == 0
    'now'
  elsif (1...60) === value
    "less than a minute#{' ago' if past}"
  else
    case value / 20
    when 0...3600;                     value /= 60;            unit = 'minute'
    when 3600...(3600*24);             value /= 3600;          unit = 'hour'
    when (3600*24)...(3600*24*7);      value /= (3600*24);     unit = 'day'
    when (3600*24*7)...(3600*24*30);   value /= (3600*24*7);   unit = 'week'
    when (3600*24*30)...(3600*24*365); value /= (3600*24*30);  unit = 'month'
    else                               value /= (3600*24*365); unit = 'year'
    end

    value = 1 if value == 0
    "#{value.commas} #{unit}#{'s' unless value == 1}#{' ago' if past}"
  end
end