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
-
#ago(t = Time.now) ⇒ Object
(also: #before)
Time in the past, i.e.
-
#days ⇒ Object
(also: #day)
24 hours in a day.
-
#filesize_format ⇒ Object
Output this number as easily readable filesize.
-
#from_now(t = Time.now) ⇒ Object
(also: #since)
Time in the future, i.e.
-
#hours ⇒ Object
(also: #hour)
60 minutes in an hour.
-
#minutes ⇒ Object
(also: #minute)
60 seconds in a minute.
-
#months ⇒ Object
(also: #month)
30 days in a month.
- #seconds ⇒ Object (also: #second)
-
#weeks ⇒ Object
(also: #week)
7 days in a week.
-
#years ⇒ Object
(also: #year)
365.25 days in a year.
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 |
#days ⇒ Object 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_format ⇒ Object
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 |
#hours ⇒ Object 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 |
#minutes ⇒ Object 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 |
#months ⇒ Object 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 |
#seconds ⇒ Object Also known as: second
7 8 9 |
# File 'lib/ramaze/snippets/numeric/time.rb', line 7 def seconds self end |
#weeks ⇒ Object 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 |
#years ⇒ Object 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 |