fat_core 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,28 @@
1
1
  module Enumerable
2
- # Yield item in groups of n
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 the
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
- class Hash
2
- # Yield each key-value pair in the Hash together with boolean flags that
3
- # indicate whether the item is the first or last yielded.
4
- def each_pair_with_flags
5
- last_k = size - 1
6
- k = 0
7
- each_pair do |key, val|
8
- first = (k == 0 ? true : false)
9
- last = (k == last_k ? true : false)
10
- yield(key, val, first, last)
11
- k += 1
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
- # Return all keys in hash that have a value == to the given value or have an
16
- # Enumerable value that includes the given value.
17
- def keys_with_value(val)
18
- result = []
19
- each_pair do |k, v|
20
- if self[k] == val || (v.respond_to?(:include?) && v.include?(val))
21
- result << k
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
- # Remove from the hash all keys that have values == to given value or that
28
- # include the given value if the hash has an Enumerable for a value
29
- def delete_with_value(v)
30
- keys_with_value(v).each do |k|
31
- delete(k)
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
- # Change each key of this Hash to its value in key_map
37
- def remap_keys(key_map = {})
38
- new_hash = {}
39
- each_pair do |key, val|
40
- if key_map.key?(key)
41
- new_hash[key_map[key]] = val
42
- else
43
- new_hash[key] = val
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
- # Change the keys of this Hash to new_keys, an array of keys
50
- def replace_keys(new_keys)
51
- unless keys.size == new_keys.size
52
- raise ArgumentError, 'replace_keys: new keys size differs from key size'
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
@@ -1,9 +1,11 @@
1
+ require 'fat_core/numeric'
2
+
1
3
  module Kernel
2
- def time_it(message = '', &block)
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 #{message} in #{run_time.secs_to_hms}"
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
- class NilClass
2
- def entitle
3
- ''
4
- end
1
+ module FatCore
2
+ module NilClass
3
+ def entitle
4
+ ''
5
+ end
5
6
 
6
- def tex_quote
7
- ''
8
- end
7
+ def tex_quote
8
+ ''
9
+ end
9
10
 
10
- def commas(_places = nil)
11
- ''
12
- end
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
@@ -1,105 +1,101 @@
1
- class Numeric
2
- def signum
3
- if positive?
4
- 1
5
- elsif negative?
6
- -1
7
- else
8
- 0
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
- def commas(places = nil)
13
- # By default, use zero places for whole numbers; four places for
14
- # numbers containing a fractional part to 4 places.
15
- if places.nil?
16
- places =
17
- if abs.modulo(1).round(4) > 0.0
18
- 4
19
- else
20
- 0
21
- end
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
- def group(places = 0, delim = ',')
27
- # Return number as a string with embedded commas
28
- # for nice printing; round to places places after
29
- # the decimal
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
- # Only convert to string numbers with exponent unless they are
32
- # less than 1 (to ensure that really small numbers round to 0.0)
33
- return to_s if abs > 1.0 && to_s =~ /e/
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
- str = to_f.round(places).to_s
46
+ str = to_f.round(places).to_s
36
47
 
37
- # Break the number into parts
38
- str =~ /^(-)?(\d*)((\.)?(\d*))?$/
39
- neg = $1 || ''
40
- whole = $2
41
- frac = $5
48
+ # Break the number into parts
49
+ str =~ /^(-)?(\d*)((\.)?(\d*))?$/
50
+ neg = $1 || ''
51
+ whole = $2
52
+ frac = $5
42
53
 
43
- # Pad out the fractional part with zeroes to the right
44
- n_zeroes = [places - frac.length, 0].max
45
- frac += '0' * n_zeroes if n_zeroes.positive?
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
- # Place the commas in the whole part only
48
- whole = whole.reverse
49
- whole.gsub!(/([0-9]{3})/, "\\1#{delim}")
50
- whole.gsub!(/#{Regexp.escape(delim)}$/, '')
51
- whole.reverse!
52
- if frac.nil? || places <= 0
53
- neg + whole
54
- else
55
- neg + whole + '.' + frac
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
- # Determine if this is a whole number.
60
- def whole?
61
- floor == self
62
- end
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
- def secs_to_hms
71
- frac = self % 1
72
- mins, secs = divmod(60)
73
- hrs, mins = mins.divmod(60)
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
- # Format the number according to the given sprintf format. Besides the
82
- # sprintf formats, a format string of '%,2', for example, will return the
83
- # number grouped by commas and rounded to 2 places. If no number of places
84
- # is given, the number will be rounded to an integer.
85
- def format_by(fmt = nil)
86
- return to_s unless fmt
87
- if /%,(?<places>\d*)/ =~ fmt.to_s.clean
88
- places ||= 0
89
- commas(places.to_i)
90
- else
91
- format fmt, self
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
- # Allow erb documents can directly interpolate numbers
96
- def tex_quote
97
- to_s
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
- class BigDecimal
102
- def inspect
103
- to_s
104
- end
105
- end
101
+ Numeric.include FatCore::Numeric