Class: Numeric

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

Overview

Carve out from lich.rbw extension to Numeric class 2024-06-13

Instance Method Summary collapse

Instance Method Details

#as_timeString

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

Examples:

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

Returns:

  • (String)

    the time representation of the numeric value.



10
11
12
# File 'lib/common/class_exts/numeric.rb', line 10

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.

Examples:

1.days # => 86400

Returns:

  • (Numeric)

    the numeric value in seconds.



58
59
60
# File 'lib/common/class_exts/numeric.rb', line 58

def days
  return self * 86400
end

#hoursNumeric Also known as: hour

Converts the numeric value to hours.

Examples:

2.hours # => 7200

Returns:

  • (Numeric)

    the numeric value in seconds.



48
49
50
# File 'lib/common/class_exts/numeric.rb', line 48

def hours
  return self * 3600
end

#minutesNumeric Also known as: minute

Converts the numeric value to minutes.

Examples:

5.minutes # => 300

Returns:

  • (Numeric)

    the numeric value in seconds.



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

def minutes
  return self * 60
end

#secondsNumeric Also known as: second

Returns the numeric value in seconds.

Examples:

5.seconds # => 5

Returns:

  • (Numeric)

    the original numeric value.



28
29
30
# File 'lib/common/class_exts/numeric.rb', line 28

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 'lib/common/class_exts/numeric.rb', line 19

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