anthroposi 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/anthroposi.rb +24 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1b38ce6b0cf22e52a24680787a12c2fc2560a4a
|
4
|
+
data.tar.gz: 6a0a14d67a090f9b86d29c15c985f1c97479c6e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6f1788445d7716b94241efb6d6a2e3a7dd12a74cb52dddbfa92a19dadd55ed91fe64cc5c9533ddd2bdab276944f951fd4c5dbdcbf51461e724cd6772ce3acb1
|
7
|
+
data.tar.gz: 7b7300da48cc8842a73ea43987c3fed5deb79186847fcd341da19676514e1da902dbbafc34d5d7d36e5fb06dfa54e34c9e0c0d0f25248a7582ab2c741ab77b11
|
data/lib/anthroposi.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# A class container for various constants and methods to convert numbers into string with SI prefixes
|
1
2
|
class Anthroposi
|
2
3
|
SI_PREFIXES = %w(K M G T P E Z Y).freeze
|
3
4
|
BINARY_UNITS = ['B'] + SI_PREFIXES.map { |p| "#{p}iB" }
|
@@ -6,6 +7,9 @@ class Anthroposi
|
|
6
7
|
SIGNIFICANT_DIGITS = 3
|
7
8
|
LARGEST_THRESHOLD = 9999
|
8
9
|
|
10
|
+
# @param bytes [Number]
|
11
|
+
# @param decimal[Boolean] optional
|
12
|
+
# @raise ArgumentError if string or number less than 1 is passed in as bytes
|
9
13
|
def initialize(bytes, decimal = false)
|
10
14
|
raise(ArgumentError, 'no string or fractional number') if bytes > 0 && bytes < 1
|
11
15
|
@bytes = bytes
|
@@ -14,6 +18,9 @@ class Anthroposi
|
|
14
18
|
@unit = compute
|
15
19
|
end
|
16
20
|
|
21
|
+
# @return [String]
|
22
|
+
# @example
|
23
|
+
# Anthroposi.new(314159).to_s #=> "314KiB"
|
17
24
|
def to_s
|
18
25
|
format @mantissa > LARGEST_THRESHOLD ? '%.2e%s' : '%s%s', @mantissa, @unit
|
19
26
|
end
|
@@ -48,15 +55,30 @@ class Anthroposi
|
|
48
55
|
end
|
49
56
|
end
|
50
57
|
|
58
|
+
# helper method to compute a number to a given number of significant digits
|
59
|
+
# @param number [Numeric]
|
60
|
+
# @param digits [Integer] number of significant digits desired
|
61
|
+
# @return [Numeric]
|
62
|
+
# @example
|
63
|
+
# significant_digits(116_067, 2) #=> 120000
|
51
64
|
def significant_digits(number, digits)
|
52
65
|
return 0 if number.zero? || digits.zero?
|
53
66
|
number.to_f.round(digits - 1 - Math.log10(number.abs).floor)
|
54
67
|
end
|
55
68
|
|
69
|
+
# wrapper function around Anthroposi class
|
70
|
+
# @param b [Integer] bytes
|
71
|
+
# @return [String] such as 3.14MiB, 234EiB
|
72
|
+
# @example
|
73
|
+
# human_size(1024) #=> "1.0KiB"
|
56
74
|
def human_size(b)
|
57
75
|
Anthroposi.new(b).to_s
|
58
76
|
end
|
59
77
|
|
60
|
-
|
61
|
-
|
78
|
+
# @param n [Numeric] number
|
79
|
+
# @return [String] such as 3.14M, 234E
|
80
|
+
# @example
|
81
|
+
# human_decimal_size(1000) #=> "1.0M"
|
82
|
+
def human_decimal_size(n)
|
83
|
+
Anthroposi.new(n, true).to_s
|
62
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anthroposi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- k z win
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ''
|
14
14
|
email: rubygems@happyw.info
|