Module: Numeric::BinaryMultipliers
- Included in:
- Numeric
- Defined in:
- lib/mega/binary_multiplier.rb
Overview
: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
Instance Method Summary collapse
-
#bit ⇒ Object
(also: #bits)
Measure in bits.
-
#byte ⇒ Object
(also: #bytes)
Bytes are converted to bits.
- #exabit ⇒ Object (also: #exabits)
- #exabyte ⇒ Object (also: #exabytes)
- #exbi ⇒ Object
- #gibi ⇒ Object
- #gigabit ⇒ Object (also: #gigabits)
- #gigabyte ⇒ Object (also: #gigabytes)
-
#in_bits ⇒ Object
Define inverse functions for bite.
-
#in_bytes ⇒ Object
Define inverse functions for bytes.
- #in_exabits ⇒ Object
- #in_exabytes ⇒ Object
- #in_gigabits ⇒ Object
- #in_gigabytes ⇒ Object
- #in_kilobits ⇒ Object
- #in_kilobytes ⇒ Object
- #in_megabits ⇒ Object
- #in_megabytes ⇒ Object
- #in_petabits ⇒ Object
- #in_petabytes ⇒ Object
- #in_terabits ⇒ Object
- #in_terabytes ⇒ Object
-
#kibi ⇒ Object
SI Binary Multiplers (also available from Multipliers).
- #kilobit ⇒ Object (also: #kilobits)
- #kilobyte ⇒ Object (also: #kilobytes)
- #mebi ⇒ Object
- #megabit ⇒ Object (also: #megabits)
- #megabyte ⇒ Object (also: #megabytes)
- #pebi ⇒ Object
- #petabit ⇒ Object (also: #petabits)
- #petabyte ⇒ Object (also: #petabytes)
-
#strfbits(fmt = '%.2f') ⇒ Object
Formated string of bits proportial to size.
-
#strfbytes(fmt = '%.2f') ⇒ Object
(also: #octet_units)
Formated string of bytes proportial to size.
- #tebi ⇒ Object
- #terabit ⇒ Object
- #terabyte ⇒ Object
Instance Method Details
#bit ⇒ Object Also known as: bits
Measure in bits.
44 |
# File 'lib/mega/binary_multiplier.rb', line 44 def bit ; self ; end |
#byte ⇒ Object Also known as: bytes
Bytes are converted to bits.
61 |
# File 'lib/mega/binary_multiplier.rb', line 61 def byte ; self * 8 ; end |
#exabit ⇒ Object Also known as: exabits
50 |
# File 'lib/mega/binary_multiplier.rb', line 50 def exabit ; self * 1024**6 ; end |
#exabyte ⇒ Object Also known as: exabytes
67 |
# File 'lib/mega/binary_multiplier.rb', line 67 def exabyte ; self * 8 * 1024**6 ; end |
#exbi ⇒ Object
83 |
# File 'lib/mega/binary_multiplier.rb', line 83 def exbi ; self * 1024**6 ; end |
#gibi ⇒ Object
80 |
# File 'lib/mega/binary_multiplier.rb', line 80 def gibi ; self * 1024**3 ; end |
#gigabit ⇒ Object Also known as: gigabits
47 |
# File 'lib/mega/binary_multiplier.rb', line 47 def gigabit ; self * 1024**3 ; end |
#gigabyte ⇒ Object Also known as: gigabytes
64 |
# File 'lib/mega/binary_multiplier.rb', line 64 def gigabyte ; self * 8 * 1024**3 ; end |
#in_bits ⇒ Object
Define inverse functions for bite
87 |
# File 'lib/mega/binary_multiplier.rb', line 87 def in_bits ; self ; end |
#in_bytes ⇒ Object
Define inverse functions for bytes
97 |
# File 'lib/mega/binary_multiplier.rb', line 97 def in_bytes ; self / 8 ; end |
#in_exabits ⇒ Object
93 |
# File 'lib/mega/binary_multiplier.rb', line 93 def in_exabits ; self / 1024**6 ; end |
#in_exabytes ⇒ Object
103 |
# File 'lib/mega/binary_multiplier.rb', line 103 def in_exabytes ; self / (8 * 1024**6) ; end |
#in_gigabits ⇒ Object
90 |
# File 'lib/mega/binary_multiplier.rb', line 90 def in_gigabits ; self / 1024**3 ; end |
#in_gigabytes ⇒ Object
100 |
# File 'lib/mega/binary_multiplier.rb', line 100 def in_gigabytes ; self / (8 * 1024**3) ; end |
#in_kilobits ⇒ Object
88 |
# File 'lib/mega/binary_multiplier.rb', line 88 def in_kilobits ; self / 1024 ; end |
#in_kilobytes ⇒ Object
98 |
# File 'lib/mega/binary_multiplier.rb', line 98 def in_kilobytes ; self / (8 * 1024) ; end |
#in_megabits ⇒ Object
89 |
# File 'lib/mega/binary_multiplier.rb', line 89 def in_megabits ; self / 1024**2 ; end |
#in_megabytes ⇒ Object
99 |
# File 'lib/mega/binary_multiplier.rb', line 99 def in_megabytes ; self / (8 * 1024**2) ; end |
#in_petabits ⇒ Object
92 |
# File 'lib/mega/binary_multiplier.rb', line 92 def in_petabits ; self / 1024**5 ; end |
#in_petabytes ⇒ Object
102 |
# File 'lib/mega/binary_multiplier.rb', line 102 def in_petabytes ; self / (8 * 1024**5) ; end |
#in_terabits ⇒ Object
91 |
# File 'lib/mega/binary_multiplier.rb', line 91 def in_terabits ; self / 1024**4 ; end |
#in_terabytes ⇒ Object
101 |
# File 'lib/mega/binary_multiplier.rb', line 101 def in_terabytes ; self / (8 * 1024**4) ; end |
#kibi ⇒ Object
SI Binary Multiplers (also available from Multipliers)
78 |
# File 'lib/mega/binary_multiplier.rb', line 78 def kibi ; self * 1024 ; end |
#kilobit ⇒ Object Also known as: kilobits
45 |
# File 'lib/mega/binary_multiplier.rb', line 45 def kilobit ; self * 1024 ; end |
#kilobyte ⇒ Object Also known as: kilobytes
62 |
# File 'lib/mega/binary_multiplier.rb', line 62 def kilobyte ; self * 8 * 1024 ; end |
#mebi ⇒ Object
79 |
# File 'lib/mega/binary_multiplier.rb', line 79 def mebi ; self * 1024**2 ; end |
#megabit ⇒ Object Also known as: megabits
46 |
# File 'lib/mega/binary_multiplier.rb', line 46 def megabit ; self * 1024**2 ; end |
#megabyte ⇒ Object Also known as: megabytes
63 |
# File 'lib/mega/binary_multiplier.rb', line 63 def megabyte ; self * 8 * 1024**2 ; end |
#pebi ⇒ Object
82 |
# File 'lib/mega/binary_multiplier.rb', line 82 def pebi ; self * 1024**5 ; end |
#petabit ⇒ Object Also known as: petabits
49 |
# File 'lib/mega/binary_multiplier.rb', line 49 def petabit ; self * 1024**5 ; end |
#petabyte ⇒ Object Also known as: petabytes
66 |
# File 'lib/mega/binary_multiplier.rb', line 66 def petabyte ; self * 8 * 1024**5 ; end |
#strfbits(fmt = '%.2f') ⇒ Object
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"
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mega/binary_multiplier.rb', line 116 def strfbits(fmt='%.2f') case fmt 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" else "#{fmt % (self.to_f / 1024**4)} tb" end end |
#strfbytes(fmt = '%.2f') ⇒ Object Also known as: octet_units
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"
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/mega/binary_multiplier.rb', line 142 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" else "#{fmt % (self.to_f / 1024**4)} TB" end end |
#tebi ⇒ Object
81 |
# File 'lib/mega/binary_multiplier.rb', line 81 def tebi ; self * 1024**4 ; end |
#terabit ⇒ Object
48 |
# File 'lib/mega/binary_multiplier.rb', line 48 def terabit ; self * 1024**4 ; end |
#terabyte ⇒ Object
65 |
# File 'lib/mega/binary_multiplier.rb', line 65 def terabyte ; self * 8 * 1024**4 ; end |