Files
chromebrew/lib/convert_size.rb
Maximilian Downey Twiss de24923ee8 Add Rubocop CI (#7083)
* Rename IgnoredPatterns to AllowedPatterns.

* Exclude docopt.rb (not our code) from Rubocop

* Disable Style/RedundantReturn

* Disable Style/MutableConstant

* Disable Style/NumericLiterals

* Set Layout/IndentationStyle to spaces

* Temporarily disable various cops.

* Add Rubocop CI via Octocop

* Lint tree with rubocop -A -c .rubocop.yml

Co-authored-by: Satadru Pramanik <satadru@gmail.com>
2022-08-22 13:31:25 -04:00

23 lines
483 B
Ruby

def human_size(bytes)
kilobyte = 1024.0
megabyte = kilobyte * kilobyte
gigabyte = megabyte * kilobyte
if bytes < kilobyte
units = 'B'
size = bytes
end
if (bytes >= kilobyte) && (bytes < megabyte)
units = 'KB'
size = bytes / kilobyte
end
if (bytes >= megabyte) && (bytes < gigabyte)
units = 'MB'
size = bytes / megabyte
end
if bytes >= gigabyte
units = 'GB'
size = bytes / gigabyte
end
return format('%.2f %s', size, units)
end