1. UNITS = %w[kbps Mbps Gbps Tbps]
  2.  
  3. def from_human(bps, unit)
  4. (UNITS.size+1).times do |round|
  5. return bps unless UNITS[round..-1].include?(unit)
  6. bps *= 1000
  7. end
  8. end
  9.  
  10. def to_human(bps)
  11. unit = "bps"
  12. (UNITS.size+1).times do |round|
  13. return "%.2f%s" % [bps, unit] if bps.abs < 1000
  14. unit = UNITS[round]
  15. bps /= 1000
  16. end
  17. end