Class: Numeric

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

Overview

Represents a numeric value with additional time-related methods.

Instance Method Summary collapse

Instance Method Details

#agoTime

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

Examples:

Get the time 60 seconds ago

60.ago # => Time.now - 60

Returns:

  • (Time)

    the time in the past



32
33
34
# File 'documented/common/class_exts/numeric.rb', line 32

def ago
  Time.now - self
end

#as_timeString

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

Examples:

Convert 3661 seconds to time

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

Returns:

  • (String)

    formatted time string



16
17
18
# File 'documented/common/class_exts/numeric.rb', line 16

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

#daysInteger Also known as: day

Returns the numeric value converted to days.

Returns:

  • (Integer)

    the numeric value in days



59
60
61
# File 'documented/common/class_exts/numeric.rb', line 59

def days
  return self * 86400
end

#hoursInteger Also known as: hour

Returns the numeric value converted to hours.

Returns:

  • (Integer)

    the numeric value in hours



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

def hours
  return self * 3600
end

#minutesInteger Also known as: minute

Returns the numeric value converted to minutes.

Returns:

  • (Integer)

    the numeric value in minutes



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

def minutes
  return self * 60
end

#secondsInteger Also known as: second

Returns the numeric value as seconds.

Returns:

  • (Integer)

    the numeric value in seconds



38
39
40
# File 'documented/common/class_exts/numeric.rb', line 38

def seconds
  return self
end

#with_commasString

Formats the numeric value with commas for thousands.

Examples:

Format a large number

1234567.with_commas # => "1,234,567"

Returns:

  • (String)

    formatted number with commas



24
25
26
# File 'documented/common/class_exts/numeric.rb', line 24

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