Class: Numeric
- Defined in:
- lib/more/facets/bytes.rb,
lib/more/facets/times.rb,
lib/core/facets/boolean.rb,
lib/more/facets/duration.rb,
lib/more/facets/multipliers.rb,
lib/core/facets/numeric/round.rb,
lib/core/facets/comparable/cmp.rb
Overview
Multipliers
Adds methods to Numeric to make working with magnitudes (kilo, mega, giga, milli, micro, etc.) as well as bits and bytes easier.
1.kilo #=> 1000
1.milli #=> 0.001
1.kibi #=> 1024
To display a value in a certain denomination, simply perform the inverse operation by placing the multiplier called on unit (1) in the denominator.
1000 / 1.kilo #=> 1
1024 / 1.kibi #=> 1
Instance Method Summary collapse
-
#after(time = ::Time.now) ⇒ Object
(also: #since, #from_now, #later)
Calculates time after a given time.
-
#approx?(x, n = 0.01) ⇒ Boolean
Determines if another number is approximately equal within a given _n_th degree.
- #atto ⇒ Object
-
#before(time = ::Time.now) ⇒ Object
(also: #until, #ago)
Calculates time before a given time.
-
#bit ⇒ Object
Bits and Bytes.
- #bits ⇒ Object
- #byte ⇒ Object
- #bytes ⇒ Object
- #centi ⇒ Object
-
#days ⇒ Object
(also: #day)
Converts days into seconds.
-
#deci ⇒ Object
SI Fractional.
-
#deka ⇒ Object
SI Multipliers.
-
#distance(other) ⇒ Object
Returns the distance between self an another value.
-
#duration(part = nil, klass = Duration) ⇒ Object
Create a Duration object using self where self could represent weeks, days, hours, minutes, and seconds.
- #exa ⇒ Object
- #exbi ⇒ Object
- #femto ⇒ Object
-
#fortnights ⇒ Object
(also: #fortnight)
Converts fortnights into seconds.
- #gibi ⇒ Object
- #giga ⇒ Object
- #hecto ⇒ Object
-
#hours ⇒ Object
(also: #hour)
Converts hours into seconds.
-
#kibi ⇒ Object
SI Binary.
- #kilo ⇒ Object
- #mebi ⇒ Object
- #mega ⇒ Object
- #micro ⇒ Object
- #milli ⇒ Object
-
#minutes ⇒ Object
(also: #minute)
Converts minutes into seconds.
-
#months ⇒ Object
(also: #month)
Converts months into seconds.
- #nano ⇒ Object
- #pebi ⇒ Object
- #peta ⇒ Object
- #pico ⇒ Object
-
#pred(n = nil) ⇒ Object
Provides #pred as the opposite of #succ.
-
#round_at(*args) ⇒ Object
See Float#round_at.
-
#round_to(*args) ⇒ Object
See Float#round_to.
-
#seconds ⇒ Object
(also: #second)
Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
-
#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.
-
#succ(n = nil) ⇒ Object
Allows #succ to take n increments.
- #tebi ⇒ Object
- #tera ⇒ Object
-
#to_b ⇒ Object
Provides a boolean interpretation of self.
-
#weekdays ⇒ Object
(also: #weekday)
Works with day in terms of weekdays.
-
#weeks ⇒ Object
(also: #week)
Converts weeks into seconds.
-
#years ⇒ Object
(also: #year)
Converts years into seconds.
Instance Method Details
#after(time = ::Time.now) ⇒ Object Also known as: since, from_now, later
Calculates time after a given time. Default time is now. Reads best with argument: 10.minutes.after(time)
112 113 114 |
# File 'lib/more/facets/times.rb', line 112 def after(time = ::Time.now) time + self end |
#approx?(x, n = 0.01) ⇒ Boolean
Determines if another number is approximately equal within a given _n_th degree. Defaults to 100ths if the degree is not specified.
CREDIT: Trans
21 22 23 24 |
# File 'lib/core/facets/numeric/round.rb', line 21 def approx?(x, n=0.01) return(self == x) if n == 0 (self - x).abs <= n end |
#atto ⇒ Object
80 |
# File 'lib/more/facets/multipliers.rb', line 80 def atto ; self.to_f / 1000000000000000000 ; end |
#before(time = ::Time.now) ⇒ Object Also known as: until, ago
Calculates time before a given time. Default time is now. Reads best with arguments: 10.days.before( Time.now - 1.day )
104 105 106 |
# File 'lib/more/facets/times.rb', line 104 def before(time = ::Time.now) time - self end |
#bit ⇒ Object
Bits and Bytes
93 |
# File 'lib/more/facets/multipliers.rb', line 93 def bit ; self ; end |
#bits ⇒ Object
76 |
# File 'lib/more/facets/bytes.rb', line 76 def bits ; self ; end |
#byte ⇒ Object
77 |
# File 'lib/more/facets/bytes.rb', line 77 def byte ; self * 8 ; end |
#bytes ⇒ Object
78 |
# File 'lib/more/facets/bytes.rb', line 78 def bytes ; self * 8 ; end |
#centi ⇒ Object
74 |
# File 'lib/more/facets/multipliers.rb', line 74 def centi ; self.to_f / 100 ; end |
#days ⇒ Object Also known as: day
Converts days into seconds.
76 |
# File 'lib/more/facets/times.rb', line 76 def days ; self * 24.hours ; end |
#deci ⇒ Object
SI Fractional
73 |
# File 'lib/more/facets/multipliers.rb', line 73 def deci ; self.to_f / 10 ; end |
#deka ⇒ Object
SI Multipliers
62 |
# File 'lib/more/facets/multipliers.rb', line 62 def deka ; self * 10 ; end |
#distance(other) ⇒ Object
Returns the distance between self an another value. This is the same as #- but it provides an alternative for common naming between variant classes.
4.distance(3) #=> 1
CREDIT Trans
67 68 69 |
# File 'lib/core/facets/comparable/cmp.rb', line 67 def distance( other ) self - other end |
#duration(part = nil, klass = Duration) ⇒ Object
Create a Duration object using self where self could represent weeks, days, hours, minutes, and seconds.
Example
10.duration(:weeks)
=> #<Duration: 10 weeks>
10.duration
=> #<Duration: 10 seconds>
472 473 474 475 476 477 478 |
# File 'lib/more/facets/duration.rb', line 472 def duration(part = nil, klass = Duration) if [:years, :months, :weeks, :days, :hours, :minutes, :seconds].include? part klass.new(part => self) else klass.new(self) end end |
#exa ⇒ Object
69 |
# File 'lib/more/facets/multipliers.rb', line 69 def exa ; self * 1000000000000000000 ; end |
#exbi ⇒ Object
89 |
# File 'lib/more/facets/multipliers.rb', line 89 def exbi ; self * 1024**6 ; end |
#femto ⇒ Object
79 |
# File 'lib/more/facets/multipliers.rb', line 79 def femto ; self.to_f / 1000000000000000 ; end |
#fortnights ⇒ Object Also known as: fortnight
Converts fortnights into seconds. (A fortnight is 2 weeks)
87 |
# File 'lib/more/facets/times.rb', line 87 def fortnights ; self * 2.weeks ; end |
#gibi ⇒ Object
86 |
# File 'lib/more/facets/multipliers.rb', line 86 def gibi ; self * 1024**3 ; end |
#giga ⇒ Object
66 |
# File 'lib/more/facets/multipliers.rb', line 66 def giga ; self * 1000000000 ; end |
#hecto ⇒ Object
63 |
# File 'lib/more/facets/multipliers.rb', line 63 def hecto ; self * 100 ; end |
#hours ⇒ Object Also known as: hour
Converts hours into seconds.
71 |
# File 'lib/more/facets/times.rb', line 71 def hours ; self * 60.minutes ; end |
#kibi ⇒ Object
SI Binary
84 |
# File 'lib/more/facets/multipliers.rb', line 84 def kibi ; self * 1024 ; end |
#kilo ⇒ Object
64 |
# File 'lib/more/facets/multipliers.rb', line 64 def kilo ; self * 1000 ; end |
#mebi ⇒ Object
85 |
# File 'lib/more/facets/multipliers.rb', line 85 def mebi ; self * 1024**2 ; end |
#mega ⇒ Object
65 |
# File 'lib/more/facets/multipliers.rb', line 65 def mega ; self * 1000000 ; end |
#micro ⇒ Object
76 |
# File 'lib/more/facets/multipliers.rb', line 76 def micro ; self.to_f / 1000000 ; end |
#milli ⇒ Object
75 |
# File 'lib/more/facets/multipliers.rb', line 75 def milli ; self.to_f / 1000 ; end |
#minutes ⇒ Object Also known as: minute
Converts minutes into seconds.
66 |
# File 'lib/more/facets/times.rb', line 66 def minutes ; self * 60 ; end |
#months ⇒ Object Also known as: month
Converts months into seconds. WARNING: This is not exact as it assumes 30 days to a month.
93 |
# File 'lib/more/facets/times.rb', line 93 def months ; self * 30.days ; end |
#nano ⇒ Object
77 |
# File 'lib/more/facets/multipliers.rb', line 77 def nano ; self.to_f / 1000000000 ; end |
#pebi ⇒ Object
88 |
# File 'lib/more/facets/multipliers.rb', line 88 def pebi ; self * 1024**5 ; end |
#peta ⇒ Object
68 |
# File 'lib/more/facets/multipliers.rb', line 68 def peta ; self * 1000000000000000 ; end |
#pico ⇒ Object
78 |
# File 'lib/more/facets/multipliers.rb', line 78 def pico ; self.to_f / 1000000000000 ; end |
#pred(n = nil) ⇒ Object
Provides #pred as the opposite of #succ.
3.pred(2) #=> 1
CREDIT Trans
88 89 90 91 |
# File 'lib/core/facets/comparable/cmp.rb', line 88 def pred(n=nil) n ||= 1 self - n end |
#round_at(*args) ⇒ Object
See Float#round_at.
5 6 7 |
# File 'lib/core/facets/numeric/round.rb', line 5 def round_at(*args) to_f.round_at(*args) end |
#round_to(*args) ⇒ Object
See Float#round_to.
11 12 13 |
# File 'lib/core/facets/numeric/round.rb', line 11 def round_to(*args) to_f.round_to(*args) end |
#seconds ⇒ Object Also known as: second
Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years. The base unit for all of these Numeric time methods is seconds.
61 |
# File 'lib/more/facets/times.rb', line 61 def seconds ; self ; 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"
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/more/facets/bytes.rb', line 115 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 |
#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"
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/more/facets/bytes.rb', line 143 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 |
#succ(n = nil) ⇒ Object
Allows #succ to take n increments.
3.succ(2) #=> 5
CREDIT Trans
77 78 79 80 |
# File 'lib/core/facets/comparable/cmp.rb', line 77 def succ(n=nil) n ||= 1 self + n end |
#tebi ⇒ Object
87 |
# File 'lib/more/facets/multipliers.rb', line 87 def tebi ; self * 1024**4 ; end |
#tera ⇒ Object
67 |
# File 'lib/more/facets/multipliers.rb', line 67 def tera ; self * 1000000000000 ; end |
#to_b ⇒ Object
Provides a boolean interpretation of self. If self == 0 then false else true.
0.to_b #=> false
1.to_b #=> true
2.3.to_b #=> true
81 82 83 |
# File 'lib/core/facets/boolean.rb', line 81 def to_b self == 0 ? false : true end |
#weekdays ⇒ Object Also known as: weekday
Works with day in terms of weekdays.
120 121 122 |
# File 'lib/more/facets/times.rb', line 120 def weekdays Weekdays.new(self) end |
#weeks ⇒ Object Also known as: week
Converts weeks into seconds.
81 |
# File 'lib/more/facets/times.rb', line 81 def weeks ; self * 7.days ; end |
#years ⇒ Object Also known as: year
Converts years into seconds.
98 |
# File 'lib/more/facets/times.rb', line 98 def years ; self * 365.days ; end |