fat_core 2.0.1 → 3.0.0
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/.ruby-version +1 -1
- data/bin/console +14 -0
- data/fat_core.gemspec +1 -0
- data/lib/fat_core/all.rb +14 -0
- data/lib/fat_core/array.rb +29 -22
- data/lib/fat_core/big_decimal.rb +12 -0
- data/lib/fat_core/date.rb +951 -938
- data/lib/fat_core/enumerable.rb +19 -3
- data/lib/fat_core/hash.rb +48 -43
- data/lib/fat_core/kernel.rb +4 -2
- data/lib/fat_core/nil.rb +13 -13
- data/lib/fat_core/numeric.rb +82 -86
- data/lib/fat_core/range.rb +164 -160
- data/lib/fat_core/string.rb +247 -242
- data/lib/fat_core/symbol.rb +17 -11
- data/lib/fat_core/version.rb +2 -2
- data/lib/fat_core.rb +0 -21
- data/spec/lib/array_spec.rb +2 -0
- data/spec/lib/big_decimal_spec.rb +7 -0
- data/spec/lib/date_spec.rb +7 -26
- data/spec/lib/enumerable_spec.rb +26 -1
- data/spec/lib/hash_spec.rb +2 -0
- data/spec/lib/kernel_spec.rb +2 -0
- data/spec/lib/nil_spec.rb +6 -0
- data/spec/lib/numeric_spec.rb +1 -5
- data/spec/lib/range_spec.rb +1 -1
- data/spec/lib/string_spec.rb +1 -6
- data/spec/lib/symbol_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -2
- metadata +9 -8
- data/lib/fat_core/boolean.rb +0 -25
- data/lib/fat_core/latex_eruby.rb +0 -11
- data/lib/fat_core/period.rb +0 -533
- data/spec/lib/period_spec.rb +0 -677
data/lib/fat_core/enumerable.rb
CHANGED
@@ -1,12 +1,28 @@
|
|
1
1
|
module Enumerable
|
2
|
-
# Yield
|
2
|
+
# Yield items in groups of n, for each group yield the group number, starting
|
3
|
+
# with zero and an Array of n items, or all remaining items if less than n.
|
4
|
+
#
|
5
|
+
# ('a'..'z').to_a.groups_of(5) do |k, grp|
|
6
|
+
# # On each iteration, grp is an Array of the next 5 items except the
|
7
|
+
# # last group, which contains only ['z'].
|
8
|
+
# end
|
3
9
|
def groups_of(n)
|
4
10
|
k = -1
|
5
11
|
group_by { k += 1; k.div(n) }
|
6
12
|
end
|
7
13
|
|
8
|
-
# Yield each item together with booleans that indicate whether the item is
|
9
|
-
# first or last in the Enumerable.
|
14
|
+
# Yield each item together with two booleans that indicate whether the item is
|
15
|
+
# the first or last item in the Enumerable.
|
16
|
+
#
|
17
|
+
# ('a'..'z').to_a.each with_flags do |letter, first, last|
|
18
|
+
# if first
|
19
|
+
# # do something special for the first item
|
20
|
+
# elsif last
|
21
|
+
# # do something special for the last item
|
22
|
+
# else
|
23
|
+
# # a middling item
|
24
|
+
# end
|
25
|
+
# end
|
10
26
|
def each_with_flags
|
11
27
|
last_k = size - 1
|
12
28
|
each_with_index do |v, k|
|
data/lib/fat_core/hash.rb
CHANGED
@@ -1,56 +1,61 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
module FatCore
|
2
|
+
|
3
|
+
module Hash
|
4
|
+
# Yield each key-value pair in the Hash together with boolean flags that
|
5
|
+
# indicate whether the item is the first or last yielded.
|
6
|
+
def each_pair_with_flags
|
7
|
+
last_k = size - 1
|
8
|
+
k = 0
|
9
|
+
each_pair do |key, val|
|
10
|
+
first = (k == 0 ? true : false)
|
11
|
+
last = (k == last_k ? true : false)
|
12
|
+
yield(key, val, first, last)
|
13
|
+
k += 1
|
14
|
+
end
|
12
15
|
end
|
13
|
-
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
# Return all keys in hash that have a value == to the given value or have an
|
18
|
+
# Enumerable value that includes the given value.
|
19
|
+
def keys_with_value(val)
|
20
|
+
result = []
|
21
|
+
each_pair do |k, v|
|
22
|
+
if self[k] == val || (v.respond_to?(:include?) && v.include?(val))
|
23
|
+
result << k
|
24
|
+
end
|
22
25
|
end
|
26
|
+
result
|
23
27
|
end
|
24
|
-
result
|
25
|
-
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
# Remove from the hash all keys that have values == to given value or that
|
30
|
+
# include the given value if the hash has an Enumerable for a value
|
31
|
+
def delete_with_value(v)
|
32
|
+
keys_with_value(v).each do |k|
|
33
|
+
delete(k)
|
34
|
+
end
|
35
|
+
self
|
32
36
|
end
|
33
|
-
self
|
34
|
-
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
# Change each key of this Hash to its value in key_map
|
39
|
+
def remap_keys(key_map = {})
|
40
|
+
new_hash = {}
|
41
|
+
each_pair do |key, val|
|
42
|
+
if key_map.key?(key)
|
43
|
+
new_hash[key_map[key]] = val
|
44
|
+
else
|
45
|
+
new_hash[key] = val
|
46
|
+
end
|
44
47
|
end
|
48
|
+
new_hash
|
45
49
|
end
|
46
|
-
new_hash
|
47
|
-
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
# Change the keys of this Hash to new_keys, an array of keys
|
52
|
+
def replace_keys(new_keys)
|
53
|
+
unless keys.size == new_keys.size
|
54
|
+
raise ArgumentError, 'replace_keys: new keys size differs from key size'
|
55
|
+
end
|
56
|
+
to_a.each_with_index.map { |(_k, v), i| [new_keys[i], v] }.to_h
|
53
57
|
end
|
54
|
-
to_a.each_with_index.map { |(_k, v), i| [new_keys[i], v] }.to_h
|
55
58
|
end
|
56
59
|
end
|
60
|
+
|
61
|
+
Hash.include FatCore::Hash
|
data/lib/fat_core/kernel.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
require 'fat_core/numeric'
|
2
|
+
|
1
3
|
module Kernel
|
2
|
-
def time_it(
|
4
|
+
def time_it(name = 'block', &block)
|
3
5
|
start = Time.now
|
4
6
|
result = yield block
|
5
7
|
run_time = Time.now - start
|
6
|
-
puts "Ran #{
|
8
|
+
puts "Ran #{name} in #{run_time.secs_to_hms}"
|
7
9
|
result
|
8
10
|
end
|
9
11
|
end
|
data/lib/fat_core/nil.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module FatCore
|
2
|
+
module NilClass
|
3
|
+
def entitle
|
4
|
+
''
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def tex_quote
|
8
|
+
''
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def format_by(_fmt)
|
15
|
-
''
|
11
|
+
def commas(_places = nil)
|
12
|
+
''
|
13
|
+
end
|
16
14
|
end
|
17
15
|
end
|
16
|
+
|
17
|
+
NilClass.include FatCore::NilClass
|
data/lib/fat_core/numeric.rb
CHANGED
@@ -1,105 +1,101 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module FatCore
|
2
|
+
module Numeric
|
3
|
+
# Return the signum function for this number, i.e., 1 for a positive number,
|
4
|
+
# 0 for zero, and -1 for a negative number.
|
5
|
+
def signum
|
6
|
+
if positive?
|
7
|
+
1
|
8
|
+
elsif negative?
|
9
|
+
-1
|
10
|
+
else
|
11
|
+
0
|
12
|
+
end
|
9
13
|
end
|
10
|
-
end
|
11
14
|
|
12
|
-
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
# Convert this number into a string and insert grouping commas into the
|
16
|
+
# whole number part and round the decimal part to +places+ decimal places,
|
17
|
+
# with the number of places being zero for an integer and 4 for a
|
18
|
+
# non-integer.
|
19
|
+
def commas(places = nil)
|
20
|
+
# By default, use zero places for whole numbers; four places for
|
21
|
+
# numbers containing a fractional part to 4 places.
|
22
|
+
if places.nil?
|
23
|
+
places =
|
24
|
+
if abs.modulo(1).round(4) > 0.0
|
25
|
+
4
|
26
|
+
else
|
27
|
+
0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
group(places, ',')
|
22
31
|
end
|
23
|
-
group(places, ',')
|
24
|
-
end
|
25
32
|
|
26
|
-
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
33
|
+
# Convert this number into a string and insert grouping delimiter character,
|
34
|
+
# +delim+ into the whole number part and round the decimal part to +places+
|
35
|
+
# decimal places, with the number of places being zero for an integer and 4
|
36
|
+
# for a non-integer.
|
37
|
+
def group(places = 0, delim = ',')
|
38
|
+
# Return number as a string with embedded commas
|
39
|
+
# for nice printing; round to places places after
|
40
|
+
# the decimal
|
30
41
|
|
31
|
-
|
32
|
-
|
33
|
-
|
42
|
+
# Only convert to string numbers with exponent unless they are
|
43
|
+
# less than 1 (to ensure that really small numbers round to 0.0)
|
44
|
+
return to_s if abs > 1.0 && to_s =~ /e/
|
34
45
|
|
35
|
-
|
46
|
+
str = to_f.round(places).to_s
|
36
47
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
48
|
+
# Break the number into parts
|
49
|
+
str =~ /^(-)?(\d*)((\.)?(\d*))?$/
|
50
|
+
neg = $1 || ''
|
51
|
+
whole = $2
|
52
|
+
frac = $5
|
42
53
|
|
43
|
-
|
44
|
-
|
45
|
-
|
54
|
+
# Pad out the fractional part with zeroes to the right
|
55
|
+
n_zeroes = [places - frac.length, 0].max
|
56
|
+
frac += '0' * n_zeroes if n_zeroes.positive?
|
46
57
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
58
|
+
# Place the commas in the whole part only
|
59
|
+
whole = whole.reverse
|
60
|
+
whole.gsub!(/([0-9]{3})/, "\\1#{delim}")
|
61
|
+
whole.gsub!(/#{Regexp.escape(delim)}$/, '')
|
62
|
+
whole.reverse!
|
63
|
+
if frac.nil? || places <= 0
|
64
|
+
neg + whole
|
65
|
+
else
|
66
|
+
neg + whole + '.' + frac
|
67
|
+
end
|
56
68
|
end
|
57
|
-
end
|
58
69
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# Return an integer type, but only if the fractional part of self
|
65
|
-
# is zero
|
66
|
-
def int_if_whole
|
67
|
-
whole? ? floor : self
|
68
|
-
end
|
70
|
+
# Return whether this is a whole number.
|
71
|
+
def whole?
|
72
|
+
floor == self
|
73
|
+
end
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
if frac.round(5) > 0.0
|
75
|
-
'%02d:%02d:%02d.%d' % [hrs, mins, secs, frac.round(5) * 100]
|
76
|
-
else
|
77
|
-
'%02d:%02d:%02d' % [hrs, mins, secs]
|
75
|
+
# Return an Integer type, but only if the fractional part of self is zero;
|
76
|
+
# otherwise just return self.
|
77
|
+
def int_if_whole
|
78
|
+
whole? ? floor : self
|
78
79
|
end
|
79
|
-
end
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
81
|
+
# Convert a number of seconds into a string of the form HH:MM:SS.dd, that is
|
82
|
+
# to hours, minutes and seconds.
|
83
|
+
def secs_to_hms
|
84
|
+
frac = self % 1
|
85
|
+
mins, secs = divmod(60)
|
86
|
+
hrs, mins = mins.divmod(60)
|
87
|
+
if frac.round(5) > 0.0
|
88
|
+
'%02d:%02d:%02d.%d' % [hrs, mins, secs, frac.round(5) * 100]
|
89
|
+
else
|
90
|
+
'%02d:%02d:%02d' % [hrs, mins, secs]
|
91
|
+
end
|
92
92
|
end
|
93
|
-
end
|
94
93
|
|
95
|
-
|
96
|
-
|
97
|
-
|
94
|
+
# Allow erb documents to directly interpolate numbers
|
95
|
+
def tex_quote
|
96
|
+
to_s
|
97
|
+
end
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
|
102
|
-
def inspect
|
103
|
-
to_s
|
104
|
-
end
|
105
|
-
end
|
101
|
+
Numeric.include FatCore::Numeric
|