#-- # Binary Multipliers # # Copyright (c) 2005 Thomas Sawyer # Copyright (c) 2004 Rich Kilmer # # Ruby License # # This module is free software. You may use, modify, and/or redistribute this # software under the same terms as Ruby. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. # # ========================================================================== # Revision History :: # -------------------------------------------------------------------------- # 2005.08.10 trans * Made a module. Added in_* methods. # 2005.07.28 trans * Rewrote lib to work in bits instead of bytes. # 2005.04.28 trans * Ported from Rich Kilmer's lib. # ========================================================================== # # :CREDIT: Credit goes to Richard Kilmer for the orignal work and # Alexander Kellett for suggesting it be included here. # #++ #:title: Binary Multipliers # # Mixin for Numeric to make working with bits and bytes easier. # These are numerous and therefore provided via a mega-module # called BinaryMultiplers, rather then individual nano-methods. # # 1.byte #=> 8 # 2.bytes #=> 16 # 1.kilobit #=> 1024 # 1.kilobyte #=> 8192 # module Numeric::BinaryMultipliers # Measure in bits. def bit ; self ; end def kilobit ; self * 1024 ; end def megabit ; self * 1024**2 ; end def gigabit ; self * 1024**3 ; end def terabit ; self * 1024**4 ; end def petabit ; self * 1024**5 ; end def exabit ; self * 1024**6 ; end alias_method :bits , :bit alias_method :kilobits, :kilobit alias_method :megabits, :megabit alias_method :gigabits, :gigabit alias_method :petabits, :petabit alias_method :exabits , :exabit # Bytes are converted to bits. def byte ; self * 8 ; end def kilobyte ; self * 8 * 1024 ; end def megabyte ; self * 8 * 1024**2 ; end def gigabyte ; self * 8 * 1024**3 ; end def terabyte ; self * 8 * 1024**4 ; end def petabyte ; self * 8 * 1024**5 ; end def exabyte ; self * 8 * 1024**6 ; end alias_method :bytes , :byte alias_method :kilobytes, :kilobyte alias_method :megabytes, :megabyte alias_method :gigabytes, :gigabyte alias_method :petabytes, :petabyte alias_method :exabytes , :exabyte # SI Binary Multiplers (also available from Multipliers) def kibi ; self * 1024 ; end def mebi ; self * 1024**2 ; end def gibi ; self * 1024**3 ; end def tebi ; self * 1024**4 ; end def pebi ; self * 1024**5 ; end def exbi ; self * 1024**6 ; end # Define inverse functions for bite def in_bits ; self ; end def in_kilobits ; self / 1024 ; end def in_megabits ; self / 1024**2 ; end def in_gigabits ; self / 1024**3 ; end def in_terabits ; self / 1024**4 ; end def in_petabits ; self / 1024**5 ; end def in_exabits ; self / 1024**6 ; end # Define inverse functions for bytes def in_bytes ; self / 8 ; end def in_kilobytes ; self / (8 * 1024) ; end def in_megabytes ; self / (8 * 1024**2) ; end def in_gigabytes ; self / (8 * 1024**3) ; end def in_terabytes ; self / (8 * 1024**4) ; end def in_petabytes ; self / (8 * 1024**5) ; end def in_exabytes ; self / (8 * 1024**6) ; end # Formated string of bits proportial to size. # # 1024.bits_to_s #=> "1.00 kb" # 1048576.bits_to_s #=> "1.00 mb" # 1073741824.bits_to_s #=> "1.00 gb" # 1099511627776.bits_to_s #=> "1.00 tb" # # Takes a format string to adjust output. # # 1024.bits_to_s('%.0f') #=> "1 kb" # def strfbits(fmt='%.2f') case when self < 1024 "#{self} bits" when self < 1024**2 "#{fmt % (self.to_f / 1024)} kb" when self < 1024**3 "#{fmt % (self.to_f / 1024**2)} mb" when self < 1024**4 "#{fmt % (self.to_f / 1024**3)} gb" when self < 1024**5 "#{fmt % (self.to_f / 1024**4)} tb" else "#{self} bits" end end # Formated string of bytes proportial to size. # # 1024.bytes_to_s #=> "1.00 KB" # 1048576.bytes_to_s #=> "1.00 MB" # 1073741824.bytes_to_s #=> "1.00 GB" # 1099511627776.bytes_to_s #=> "1.00 TB" # # Takes a format string to adjust output. # # 1024.bytes_to_s('%.0f') #=> "1 KB" # def strfbytes(fmt='%.2f') case when self < 1024 "#{self} bytes" when self < 1024**2 "#{fmt % (self.to_f / 1024)} KB" when self < 1024**3 "#{fmt % (self.to_f / 1024**2)} MB" when self < 1024**4 "#{fmt % (self.to_f / 1024**3)} GB" when self < 1024**5 "#{fmt % (self.to_f / 1024**4)} TB" else "#{self} bytes" end end # deprecated alias_method :octet_units, :strfbytes end #-- class Numeric include BinaryMultipliers end #++ # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin testing require 'test/unit' class TC_Numeric < Test::Unit::TestCase # bits def test_bits assert_equal( 8, 8.bits ) end def test_kilobits assert_equal( 1024**1, 1.kilobit ) end def test_megabits assert_equal( 1024**2, 1.megabit ) end def test_gigabits assert_equal( 1024**3, 1.gigabit ) end def test_terabits assert_equal( 1024**4, 1.terabit ) end # bytes def test_bytes assert_equal( 8192, 1024.bytes ) end def test_kilobytes assert_equal( 1024**1*8, 1.kilobyte ) end def test_megabytes assert_equal( 1024**2*8, 1.megabyte ) end def test_gigabytes assert_equal( 1024**3*8, 1.gigabyte ) end def test_terabytes assert_equal( 1024**4*8, 1.terabyte ) end # bits_to_s def test_strfbits assert_equal( "1.00 kb", 1024.strfbits ) assert_equal( "1.00 mb", 1048576.strfbits ) assert_equal( "1.00 gb", 1073741824.strfbits ) assert_equal( "1.00 tb", 1099511627776.strfbits ) end # bytes_to_s def test_strfbytes assert_equal( "1.00 KB", 1024.strfbytes ) assert_equal( "1.00 MB", 1048576.strfbytes ) assert_equal( "1.00 GB", 1073741824.strfbytes ) assert_equal( "1.00 TB", 1099511627776.strfbytes ) end end =end