Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
documented/common/class_exts/numeric.rb

Overview

Extends the Numeric class with additional time-related methods.

Examples:

Using Numeric extensions

120.as_time # => "2:00:00"
5.minutes # => 300

Instance Method Summary collapse

Instance Method Details

#agoTime

Returns the time that was the given number of seconds ago from now.

Examples:

3600.ago # => Time.now - 3600

Returns:

  • (Time)

    The time in the past.



27
28
29
# File 'documented/common/class_exts/numeric.rb', line 27

def ago
  Time.now - self
end

#as_timeString

Converts the numeric value to a time string in “HH:MM:SS” format.

Examples:

3661.as_time # => "1:01:01"

Returns:

  • (String)

    The formatted time string.



11
12
13
# File 'documented/common/class_exts/numeric.rb', line 11

def as_time
  sprintf("%d:%02d:%02d", (self / 60).truncate, self.truncate % 60, ((self % 1) * 60).truncate)
end

#daysNumeric Also known as: day

Converts the numeric value to days in seconds.

Examples:

1.day # => 86400

Returns:

  • (Numeric)

    The numeric value in seconds.



62
63
64
# File 'documented/common/class_exts/numeric.rb', line 62

def days
  return self * 86400
end

#hoursNumeric Also known as: hour

Converts the numeric value to hours in seconds.

Examples:

2.hours # => 7200

Returns:

  • (Numeric)

    The numeric value in seconds.



53
54
55
# File 'documented/common/class_exts/numeric.rb', line 53

def hours
  return self * 3600
end

#minutesNumeric Also known as: minute

Converts the numeric value to minutes in seconds.

Examples:

5.minutes # => 300

Returns:

  • (Numeric)

    The numeric value in seconds.



44
45
46
# File 'documented/common/class_exts/numeric.rb', line 44

def minutes
  return self * 60
end

#secondsNumeric Also known as: second

Returns the numeric value as seconds.

Examples:

5.seconds # => 5

Returns:

  • (Numeric)

    The numeric value itself.



35
36
37
# File 'documented/common/class_exts/numeric.rb', line 35

def seconds
  return self
end

#with_commasString

Formats the numeric value with commas as thousands separators.

Examples:

1000000.with_commas # => "1,000,000"

Returns:

  • (String)

    The numeric value as a string with commas.



19
20
21
# File 'documented/common/class_exts/numeric.rb', line 19

def with_commas
  self.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
end