1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# frozen_string_literal: true
require 'delegate'
# Aruba
module Aruba
# File Size
class FileSize
include Comparable
private
attr_reader :bytes, :divisor
public
# Create file size object
def initialize(bytes)
@bytes = bytes
@divisor = 1024
end
# Convert to bytes
def to_byte
bytes
end
alias to_i to_byte
# Convert to float
def to_f
to_i.to_f
end
# Convert to string
def to_s
to_i.to_s
end
alias inspect to_s
# Move to other
def coerce(other)
[bytes, other]
end
# Convert to kibi byte
def to_kibi_byte
to_byte.to_f / divisor
end
# Convert to mebi byte
def to_mebi_byte
to_kibi_byte.to_f / divisor
end
# Convert to gibi byte
def to_gibi_byte
to_mebi_byte.to_f / divisor
end
# Compare size with other size
def <=>(other)
to_i <=> other.to_i
end
end
end
|