class Nomad::Size

Convention of upper vs lower

Constants

BIT
BYTE
EXABIT
EXABYTE
GIGABIT
GIGABYTE
KILOBIT
KILOBYTE
LABELS_MAP
LABEL_BIT
LABEL_BYTE
LABEL_EXABIT
LABEL_EXABYTE
LABEL_GIGABIT
LABEL_GIGABYTE
LABEL_KILOBIT
LABEL_KILOBYTE
LABEL_MEGABIT
LABEL_MEGABYTE
LABEL_PETABIT
LABEL_PETABYTE
LABEL_TERABIT
LABEL_TERABYTE
LABEL_YOTTABIT
LABEL_YOTTABYTE
LABEL_ZETTABIT
LABEL_ZETTABYTE
MEGABIT
MEGABYTE
PETABIT
PETABYTE
TERABIT
TERABYTE
YOTTABIT
YOTTABYTE
ZETTABIT
ZETTABYTE

Public Class Methods

new(b) click to toggle source

Initialize accepts the numer of bits as an Integer or Float and builds the conversions around it.

@example

Size.new(1342902)

@example More human friendly

Size.new(3*Size::KILOBYTES) # 3 KB
# File lib/nomad/size.rb, line 77
def initialize(b)
  @b = Float(b || 0)
end

Public Instance Methods

bits() click to toggle source
# File lib/nomad/size.rb, line 81
def bits
  @b / BIT
end
bytes() click to toggle source
# File lib/nomad/size.rb, line 85
def bytes
  @b / BYTE
end
exabits() click to toggle source
# File lib/nomad/size.rb, line 129
def exabits
  @b / EXABIT
end
exabytes() click to toggle source
# File lib/nomad/size.rb, line 133
def exabytes
  @b / EXABYTE
end
gigabits() click to toggle source
# File lib/nomad/size.rb, line 105
def gigabits
  @b / GIGABIT
end
gigabytes() click to toggle source
# File lib/nomad/size.rb, line 109
def gigabytes
  @b / GIGABYTE
end
inspect() click to toggle source
# File lib/nomad/size.rb, line 180
def inspect
  "#<%s:0x%s %s>" % [self.class, (object_id << 1).to_s(16), "@size=\"#{to_s}\""]
end
kilobits() click to toggle source
# File lib/nomad/size.rb, line 89
def kilobits
  @b / KILOBIT
end
kilobytes() click to toggle source
# File lib/nomad/size.rb, line 93
def kilobytes
  @b / KILOBYTE
end
megabits() click to toggle source
# File lib/nomad/size.rb, line 97
def megabits
  @b / MEGABIT
end
megabytes() click to toggle source
# File lib/nomad/size.rb, line 101
def megabytes
  @b / MEGABYTE
end
petabits() click to toggle source
# File lib/nomad/size.rb, line 121
def petabits
  @b / PETABIT
end
petabytes() click to toggle source
# File lib/nomad/size.rb, line 125
def petabytes
  @b / PETABYTE
end
terabits() click to toggle source
# File lib/nomad/size.rb, line 113
def terabits
  @b / TERABIT
end
terabytes() click to toggle source
# File lib/nomad/size.rb, line 117
def terabytes
  @b / TERABYTE
end
to_s() click to toggle source

The “human-friendly” form of this duration. Large values are rounded off, sacrificing correctness for readability. The underlying data is still correct and the real value can be retrieved by calling another method.

@example

size.to_s #=> "5GB"

@return [String]

# File lib/nomad/size.rb, line 161
def to_s
  b, negative = @b, false
  if b < 0
    b *= -1
    negative = true
  end

  biggest = LABELS_MAP.find { |l,c| b / c >= 1 }

  if biggest.nil?
    return String(b.round(0)) << LABEL_BIT
  end

  result = String((b / biggest[1]).round(0)) << biggest[0]

  return "-" << result if negative
  return result
end
yottabits() click to toggle source
# File lib/nomad/size.rb, line 145
def yottabits
  @b / YOTTABIT
end
yottabytes() click to toggle source
# File lib/nomad/size.rb, line 149
def yottabytes
  @b / YOTTABYTE
end
zettabits() click to toggle source
# File lib/nomad/size.rb, line 137
def zettabits
  @b / ZETTABIT
end
zettabytes() click to toggle source
# File lib/nomad/size.rb, line 141
def zettabytes
  @b / ZETTABYTE
end