Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/tagen/core/time.rb,
lib/tagen/core/numeric.rb

Instance Method Summary collapse

Instance Method Details

#div2(num) ⇒ Numeric

equivalent to ‘a.fdiv(b).ceil`. +1

Parameters:

Returns:



6
7
8
9
# File 'lib/tagen/core/numeric.rb', line 6

def div2(num)
	d, m = divmod(num)
	d + (m==0 ? 0 : 1)
end

#time_humanize(include_seconds = false) ⇒ Object

Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.

Examples:


1.time_humanize(true)    -> 1 seconds
36561906.time_humanize   -> 1 years 2 months 3 days 4 hours 5 minutes


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tagen/core/time.rb', line 22

def time_humanize(include_seconds=false)
  deta = self
  deta, seconds = deta.divmod(60)
  deta, minutes = deta.divmod(60)
  deta, hours = deta.divmod(24)
  deta, days = deta.divmod(30)
  years, months = deta.divmod(12)

  ret = ""
  ret << "#{years} years " unless years == 0
  ret << "#{months} months " unless months == 0
  ret << "#{days} days " unless days == 0
  ret << "#{hours} hours " unless hours == 0
  ret << "#{minutes} minutes " unless minutes == 0
  ret << "#{seconds} seconds" if include_seconds

  ret.rstrip
end