Class: Numeric

Inherits:
Object
  • Object
show all
Includes:
Time::Units
Defined in:
lib/familia/core_ext.rb

Overview

Extends the Numeric class with time unit and byte conversion functionality

Constant Summary

Constants included from Time::Units

Time::Units::PER_DAY, Time::Units::PER_HOUR, Time::Units::PER_MICROSECOND, Time::Units::PER_MILLISECOND, Time::Units::PER_MINUTE

Instance Method Summary collapse

Methods included from Time::Units

#days, #hours, #in_days, #in_hours, #in_microseconds, #in_milliseconds, #in_minutes, #in_seconds, #in_time, #in_weeks, #in_years, #microseconds, #milliseconds, #minutes, #seconds, #weeks, #years

Instance Method Details

#to_bytesString

Converts the number to a human-readable byte representation using binary units

Examples:

1024.to_bytes      #=> "1.00 KiB"
2_097_152.to_bytes #=> "2.00 MiB"
3_221_225_472.to_bytes #=> "3.00 GiB"

Returns:

  • (String)

    A string representing the number in bytes, KiB, MiB, GiB, or TiB



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/familia/core_ext.rb', line 123

def to_bytes
  units = %w[B KiB MiB GiB TiB]
  size = abs.to_f
  unit = 0

  while size >= 1024 && unit < units.length - 1
    size /= 1024
    unit += 1
  end

  format('%3.2f %s', size, units[unit])
end

#to_msFloat

Converts the number to milliseconds

Returns:

  • (Float)

    The number in milliseconds



110
111
112
# File 'lib/familia/core_ext.rb', line 110

def to_ms
  (self * 1000.to_f)
end