Module: Ramaze::CoreExtensions::Numeric

Defined in:
lib/ramaze/snippets/numeric/time.rb,
lib/ramaze/snippets/numeric/filesize_format.rb

Overview

Extensions for Numeric

Constant Summary collapse

FILESIZE_FORMAT =
[
  ['%.1fT', 1 << 40],
  ['%.1fG', 1 << 30],
  ['%.1fM', 1 << 20],
  ['%.1fK', 1 << 10],
]

Instance Method Summary collapse

Instance Method Details

#ago(t = Time.now) ⇒ Object Also known as: before

Time in the past, i.e. 3.days.ago



49
50
51
# File 'lib/ramaze/snippets/numeric/time.rb', line 49

def ago t = Time.now
  t - self
end

#daysObject Also known as: day

24 hours in a day



25
26
27
# File 'lib/ramaze/snippets/numeric/time.rb', line 25

def days
  self * 86400
end

#filesize_formatObject

Output this number as easily readable filesize. Usage:

100_000.filesize_format             # => "97.7K"
100_000_000.filesize_format         # => "95.4M"
100_000_000_000.filesize_format     # => "93.1G"
100_000_000_000_000.filesize_format # => "90.9T"


22
23
24
25
26
27
28
# File 'lib/ramaze/snippets/numeric/filesize_format.rb', line 22

def filesize_format
  FILESIZE_FORMAT.each do |format, size|
    return format % (self.to_f / size) if self >= size
  end

  self.to_s
end

#from_now(t = Time.now) ⇒ Object Also known as: since

Time in the future, i.e. 3.days.from_now



55
56
57
# File 'lib/ramaze/snippets/numeric/time.rb', line 55

def from_now t = Time.now
  t + self
end

#hoursObject Also known as: hour

60 minutes in an hour



19
20
21
# File 'lib/ramaze/snippets/numeric/time.rb', line 19

def hours
  self * 3600
end

#minutesObject Also known as: minute

60 seconds in a minute



13
14
15
# File 'lib/ramaze/snippets/numeric/time.rb', line 13

def minutes
  self * 60
end

#monthsObject Also known as: month

30 days in a month



37
38
39
# File 'lib/ramaze/snippets/numeric/time.rb', line 37

def months
  self * 2592000
end

#secondsObject Also known as: second



7
8
9
# File 'lib/ramaze/snippets/numeric/time.rb', line 7

def seconds
  self
end

#weeksObject Also known as: week

7 days in a week



31
32
33
# File 'lib/ramaze/snippets/numeric/time.rb', line 31

def weeks
  self * 604800
end

#yearsObject Also known as: year

365.25 days in a year



43
44
45
# File 'lib/ramaze/snippets/numeric/time.rb', line 43

def years
  self * 31557600
end