module UsefulUtilities::Size::Bit

Possible units:

:bit  - bits
:kbit - kilobits
:Mbit - megabits
:Gbit - gigabits
:Tbit - terabits

Used SI standard en.wikipedia.org/wiki/Binary_prefix

Decimal
1 K = 1000

Public Instance Methods

bits_per_sec_to_human_readable(value_in_bits) click to toggle source

@param value_in_bits [Integer] value in bits @return [String] human readable value

# File lib/useful_utilities/size/bit.rb, line 84
def bits_per_sec_to_human_readable(value_in_bits)
  unit  = suitable_unit_for_bits(value_in_bits)
  value = bits_to_human_size(value_in_bits, unit.to_sym)

  "#{value} #{unit}/s"
end
bits_to_human_size(size, unit, rounder = 3) click to toggle source

@param size [Numeric] @param unit [Symbol] @param rounder [Integer] @return [Numeric] humanized size in provided unit

# File lib/useful_utilities/size/bit.rb, line 56
def bits_to_human_size(size, unit, rounder = 3)
  case unit
  when :bit  then size.round(rounder)
  when :kbit then to_kilobits(size, :bit).round(rounder)
  when :Mbit then to_megabits(size, :bit).round(rounder)
  when :Gbit then to_gigabits(size, :bit).round(rounder)
  when :Tbit then to_terabits(size, :bit).round(rounder)
  else  unsupported_unit!(unit)
  end
end
suitable_unit_for_bits(value) click to toggle source

@param value [Integer] @return [Symbol] suitable unit @raise [ArgumentError] if value is not integer or less than zero

# File lib/useful_utilities/size/bit.rb, line 70
def suitable_unit_for_bits(value)
  return not_integer!(value)          unless value.is_a?(Integer)
  return not_positive_integer!(value) unless value > -1

  if value.to_s.length    <= 3  then :bit
  elsif value.to_s.length <= 6  then :kbit
  elsif value.to_s.length <= 9  then :Mbit
  elsif value.to_s.length <= 12 then :Gbit
  else :Tbit
  end
end
to_bits(size, unit) click to toggle source

@param size [Numeric] @param unit [Symbol] @return [Numeric] size in bits

# File lib/useful_utilities/size/bit.rb, line 48
def to_bits(size, unit)
  to_decimal_bi(size, bit_prefix(unit))
end
to_gigabits(size, unit) click to toggle source

@param size [Numeric] @param unit [Symbol] @return [Numeric] size in gigabits

# File lib/useful_utilities/size/bit.rb, line 27
def to_gigabits(size, unit)
  to_giga(size, bit_prefix(unit))
end
to_kilobits(size, unit) click to toggle source

@param size [Numeric] @param unit [Symbol] @return [Numeric] size in kilobits

# File lib/useful_utilities/size/bit.rb, line 41
def to_kilobits(size, unit)
  to_kilo(size, bit_prefix(unit))
end
to_megabits(size, unit) click to toggle source

@param size [Numeric] @param unit [Symbol] @return [Numeric] size in megabits

# File lib/useful_utilities/size/bit.rb, line 34
def to_megabits(size, unit)
  to_mega(size, bit_prefix(unit))
end
to_terabits(size, unit) click to toggle source

@param size [Numeric] @param unit [Symbol] @return [Numeric] size in terabits

# File lib/useful_utilities/size/bit.rb, line 20
def to_terabits(size, unit)
  to_tera(size, bit_prefix(unit))
end

Private Instance Methods

bit_prefix(unit) click to toggle source
# File lib/useful_utilities/size/bit.rb, line 93
def bit_prefix(unit)
  case unit
  when :bit  then :B
  when :kbit then :KB
  when :Mbit then :MB
  when :Gbit then :GB
  when :Tbit then :TB
  else unsupported_unit!(unit)
  end
end
not_integer!(value) click to toggle source
# File lib/useful_utilities/size/bit.rb, line 108
def not_integer!(value)
  raise ArgumentError.new("#{value } is not integer")
end
not_positive_integer!(value) click to toggle source
# File lib/useful_utilities/size/bit.rb, line 112
def not_positive_integer!(value)
  raise ArgumentError.new("#{value } is not positive integer")
end
unsupported_unit!(unit) click to toggle source
# File lib/useful_utilities/size/bit.rb, line 104
def unsupported_unit!(unit)
  raise ArgumentError.new("Unsupported unit - #{ unit }")
end