utilities 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/utilities/array.rb +39 -0
- data/lib/utilities/datetime.rb +6 -0
- data/lib/utilities/enumerable.rb +19 -0
- data/lib/utilities/hash.rb +18 -0
- data/lib/utilities/kernel.rb +6 -0
- data/lib/utilities/numeric.rb +40 -0
- data/lib/utilities/object.rb +9 -0
- data/lib/utilities/range.rb +24 -0
- data/lib/utilities/string.rb +11 -0
- data/lib/utilities/time.rb +7 -0
- data/lib/utilities/utilities.rb +169 -0
- data/lib/version.rb +1 -1
- data/utilities-0.0.14.gem +0 -0
- data/utilities.gemspec +1 -7
- metadata +14 -2
@@ -0,0 +1,39 @@
|
|
1
|
+
class Array
|
2
|
+
# Returns true if the array contains only numerical values
|
3
|
+
def numerics?( allow_nil = false )
|
4
|
+
(allow_nil ? compact : self).reject{ |x| x.is_a?( Numeric ) }.empty?
|
5
|
+
end
|
6
|
+
alias_method :numeric?, :numerics?
|
7
|
+
alias_method :narray?, :numerics?
|
8
|
+
|
9
|
+
# Transforms an array
|
10
|
+
def to_numerics
|
11
|
+
map{ |x| x.to_f }
|
12
|
+
end
|
13
|
+
alias_method :to_numeric, :to_numerics
|
14
|
+
alias_method :to_narray, :to_numerics
|
15
|
+
|
16
|
+
# Returns a copy of self reverse sorted
|
17
|
+
def reverse_sort
|
18
|
+
dup.rsort!
|
19
|
+
end
|
20
|
+
alias_method :rsort, :reverse_sort
|
21
|
+
|
22
|
+
# Reverse sort self
|
23
|
+
def reverse_sort!
|
24
|
+
sort!{|x,y| y <=> x }
|
25
|
+
end
|
26
|
+
alias_method :rsort!, :reverse_sort!
|
27
|
+
|
28
|
+
# Returns a copy of self that includes the Statistics methods
|
29
|
+
def to_stat
|
30
|
+
dup.to_stat!
|
31
|
+
end
|
32
|
+
alias_method :to_stats, :to_stat
|
33
|
+
|
34
|
+
# Adds the statistics methods to self
|
35
|
+
def to_stat!
|
36
|
+
extend(Utilities::Statistics)
|
37
|
+
end
|
38
|
+
alias_method :to_stats!, :to_stat!
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Collects the first N truthy values
|
3
|
+
# {}.collect_first {|a, b| obj} => obj
|
4
|
+
# {}.collect_first(n) {|a, b| obj} => [obj]
|
5
|
+
def collect_first( amount = nil, &block )
|
6
|
+
array = !amount.nil?
|
7
|
+
amount ||= 1
|
8
|
+
|
9
|
+
values = []
|
10
|
+
self.each do |val|
|
11
|
+
break if values.length >= amount
|
12
|
+
t = yield(val)
|
13
|
+
values << t if t
|
14
|
+
end
|
15
|
+
|
16
|
+
!array && amount == 1 ? values.first : values
|
17
|
+
end
|
18
|
+
alias_method :map_first, :collect_first
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns a new hash with the results of running block once for every key in self
|
3
|
+
def collect_keys
|
4
|
+
each_with_object({}){ |(k,v),h| h[yield(k)] = v } if block_given?
|
5
|
+
end
|
6
|
+
alias_method :map_keys, :collect_keys
|
7
|
+
|
8
|
+
# Returns a new hash with the results of running block once for every value in self
|
9
|
+
def collect_values
|
10
|
+
each_with_object({}){ |(k,v),h| h[k] = yield(v) } if block_given?
|
11
|
+
end
|
12
|
+
alias_method :map_values, :collect_values
|
13
|
+
|
14
|
+
# Returns a new hash where all keys have been symbolized
|
15
|
+
def symbolize_keys
|
16
|
+
collect_keys{ |k| k.to_sym }
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class Numeric
|
2
|
+
# Convert to degrees
|
3
|
+
def degrees
|
4
|
+
self * Math::PI / 180
|
5
|
+
end
|
6
|
+
|
7
|
+
# Return the square of self
|
8
|
+
def square
|
9
|
+
self * self
|
10
|
+
end
|
11
|
+
|
12
|
+
#Transform self to a string formatted time (HH:MM) Ex: 14.5 => “14:30“
|
13
|
+
def hour_to_string delimiter = ':'
|
14
|
+
hour = self.to_i
|
15
|
+
min = "%02d" % (self.abs * 60 % 60).to_i
|
16
|
+
"#{hour}#{delimiter}#{min}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return the square root of self
|
20
|
+
def sqrt
|
21
|
+
Math.sqrt(self)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Calculate the rank of self based on provided min and max
|
25
|
+
def rank min, max
|
26
|
+
s, min, max = self.to_f, min.to_f, max.to_f
|
27
|
+
min == max ? 0.0 : (s - min) / (max - min)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Transforms self to a string with *decimals* decimals
|
31
|
+
def to_decimals decimals = 2
|
32
|
+
"%.#{decimals}f" % self
|
33
|
+
end
|
34
|
+
|
35
|
+
# Calculate the percentage of self on n
|
36
|
+
def percentage_of n, t = 100
|
37
|
+
n == 0 ? 0.0 : self / n.to_f * t
|
38
|
+
end
|
39
|
+
alias_method :percent_of, :percentage_of
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Range
|
2
|
+
# Return a range containing elements common to the two ranges, with no duplicates
|
3
|
+
def intersection range
|
4
|
+
values = self.to_a & range.to_a
|
5
|
+
values.empty? ? nil : (values.first..values.last)
|
6
|
+
end
|
7
|
+
alias_method :&, :intersection
|
8
|
+
|
9
|
+
# Verify if self is empty
|
10
|
+
def empty?
|
11
|
+
count.zero?
|
12
|
+
end
|
13
|
+
|
14
|
+
# Detect if the two ranges overlap one with the other
|
15
|
+
def overlap? range
|
16
|
+
!(self & range).nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
# Adds cover? if not defined (like in previous rubies)
|
20
|
+
def cover? object
|
21
|
+
ends = [self.first, self.last]
|
22
|
+
ends.min <= object && object <= ends.max
|
23
|
+
end unless self.instance_methods.include?(:cover?)
|
24
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
module Utilities
|
2
|
+
module Submodules
|
3
|
+
# Find every submodules of an object.
|
4
|
+
def submodules
|
5
|
+
constants.collect{ |const_name| const_get(const_name) }.select{ |const| const.class == Module }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Subclasses
|
10
|
+
# Find every subclass of an object.
|
11
|
+
def subclasses(direct = false)
|
12
|
+
classes = []
|
13
|
+
|
14
|
+
if direct
|
15
|
+
ObjectSpace.each_object(Class) do |c|
|
16
|
+
classes << c if c.superclass == self
|
17
|
+
end
|
18
|
+
else
|
19
|
+
ObjectSpace.each_object(Class) do |c|
|
20
|
+
classes << c if c.ancestors.include?(self) and (c != self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
classes
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Statistics
|
29
|
+
# Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
|
30
|
+
def sum
|
31
|
+
flatten.compact.inject( :+ )
|
32
|
+
end
|
33
|
+
|
34
|
+
# Calculate squares of each item
|
35
|
+
def squares
|
36
|
+
map{ |i| i**2 }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return a new array containing the rank of each value
|
40
|
+
# Ex: [1, 2, 2, 8, 9] #=> [0.0, 1.5, 1.5, 3.0, 4.0]
|
41
|
+
def ranks( already_sorted = false )
|
42
|
+
a = already_sorted ? self : sort
|
43
|
+
map{ |i| (a.index(i) + a.rindex(i)) / 2.0 }
|
44
|
+
end
|
45
|
+
|
46
|
+
# Calculate square roots of each item
|
47
|
+
def sqrts
|
48
|
+
map{ |i| i.sqrt }
|
49
|
+
end
|
50
|
+
|
51
|
+
# Calculate the mean of the array, as long as all objects respond to / operator
|
52
|
+
def mean
|
53
|
+
a = flatten.compact.to_stat
|
54
|
+
(a.size > 0) ? a.sum.to_f / a.size : 0.0
|
55
|
+
end
|
56
|
+
alias_method :average, :mean
|
57
|
+
|
58
|
+
# Calculate the number of occurences for each element of the array
|
59
|
+
def frequences
|
60
|
+
inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
61
|
+
end
|
62
|
+
|
63
|
+
# Return the variance of self
|
64
|
+
def variance
|
65
|
+
m = mean
|
66
|
+
inject(0) { |v, x| v += (x - m) ** 2 }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Return the (sample|population) standard deviation of self
|
70
|
+
# If population is set to true, then we consider the dataset as the complete population
|
71
|
+
# Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
|
72
|
+
def standard_deviation( population = false )
|
73
|
+
size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
|
74
|
+
end
|
75
|
+
alias_method :std_dev, :standard_deviation
|
76
|
+
|
77
|
+
# Return the median of sorted self
|
78
|
+
def median( already_sorted = false )
|
79
|
+
return nil if empty?
|
80
|
+
a = sort_and_extend( already_sorted )
|
81
|
+
m_pos = size / 2
|
82
|
+
size % 2 == 1 ? a[m_pos] : (a[m_pos-1] + a[m_pos]).to_f / 2
|
83
|
+
end
|
84
|
+
alias_method :second_quartile, :median
|
85
|
+
|
86
|
+
# Return the first quartile of self
|
87
|
+
def first_quartile( already_sorted = false )
|
88
|
+
return nil if size < 4
|
89
|
+
a = already_sorted ? self : sort
|
90
|
+
a[0..((size / 2) - 1)].extend(Utilities::Statistics).median( true )
|
91
|
+
end
|
92
|
+
alias_method :lower_quartile, :first_quartile
|
93
|
+
|
94
|
+
# Return the last quartile of self
|
95
|
+
def last_quartile( already_sorted = false )
|
96
|
+
return nil if size < 4
|
97
|
+
a = already_sorted ? self : sort
|
98
|
+
a[((size / 2) + 1)..-1].extend(Utilities::Statistics).median( true )
|
99
|
+
end
|
100
|
+
alias_method :upper_quartile, :last_quartile
|
101
|
+
|
102
|
+
# Return an array containing the first, the second and the last quartile of self
|
103
|
+
def quartiles( already_sorted = false )
|
104
|
+
a = sort_and_extend( already_sorted )
|
105
|
+
[a.first_quartile( true ), a.median( true ), a.last_quartile( true )]
|
106
|
+
end
|
107
|
+
|
108
|
+
# Calculate the interquartile range of self
|
109
|
+
def interquartile_range( already_sorted = false )
|
110
|
+
return nil if size < 4
|
111
|
+
a = sort_and_extend( already_sorted )
|
112
|
+
a.last_quartile - a.first_quartile
|
113
|
+
end
|
114
|
+
|
115
|
+
# Return an array of modes with their corresponding occurences
|
116
|
+
def modes
|
117
|
+
fre = frequences
|
118
|
+
max = freq.values.max
|
119
|
+
fre.select{ |k, f| f == max }
|
120
|
+
end
|
121
|
+
|
122
|
+
# Return the midrange of sorted self
|
123
|
+
def midrange( already_sorted = false )
|
124
|
+
return nil if empty?
|
125
|
+
a = sort_and_extend( already_sorted )
|
126
|
+
(a.first + a.last) / 2.0
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return the statistical range of sorted self
|
130
|
+
def statistical_range( already_sorted = false )
|
131
|
+
return nil if empty?
|
132
|
+
a = sort_and_extend( already_sorted )
|
133
|
+
(a.last - a.first)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Return all statistics from self in a simple hash
|
137
|
+
def statistics( already_sorted = false )
|
138
|
+
sorted = sort_and_extend( already_sorted )
|
139
|
+
|
140
|
+
{
|
141
|
+
:first => self.first,
|
142
|
+
:last => self.last,
|
143
|
+
:size => self.size,
|
144
|
+
:sum => self.sum,
|
145
|
+
:min => self.min,
|
146
|
+
:max => self.max,
|
147
|
+
:mean => self.mean,
|
148
|
+
:frequences => self.frequences,
|
149
|
+
:variance => self.variance,
|
150
|
+
:standard_deviation => self.standard_deviation,
|
151
|
+
:modes => self.modes,
|
152
|
+
|
153
|
+
# Need to be sorted...
|
154
|
+
:ranks => sorted.ranks( true ),
|
155
|
+
:median => sorted.median( true ),
|
156
|
+
:midrange => sorted.midrange( true ),
|
157
|
+
:statistical_range => sorted.statistical_range( true ),
|
158
|
+
:quartiles => sorted.quartiles( true ),
|
159
|
+
:interquartile_range => sorted.interquartile_range( true )
|
160
|
+
}
|
161
|
+
end
|
162
|
+
alias_method :stats, :statistics
|
163
|
+
|
164
|
+
protected
|
165
|
+
def sort_and_extend( already_sorted = false )
|
166
|
+
already_sorted ? self : sort.extend(Utilities::Statistics)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
data/lib/version.rb
CHANGED
Binary file
|
data/utilities.gemspec
CHANGED
@@ -14,13 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.summary = "Few utilities include in all my projects"
|
15
15
|
s.description = "Few utilities include in all my projects, including a module for statistics, some to_date and to_time functions as well as intersection method for Range object."
|
16
16
|
|
17
|
-
s.files =
|
18
|
-
'LICENSE',
|
19
|
-
'README',
|
20
|
-
'utilities.gemspec',
|
21
|
-
'lib/utilities.rb',
|
22
|
-
'lib/version.rb',
|
23
|
-
'test/test_utilities.rb']
|
17
|
+
s.files = `git ls-files`.split("\n")
|
24
18
|
|
25
19
|
s.require_paths = ['lib', 'test']
|
26
20
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,10 +24,22 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- LICENSE
|
26
26
|
- README
|
27
|
-
- utilities.gemspec
|
28
27
|
- lib/utilities.rb
|
28
|
+
- lib/utilities/array.rb
|
29
|
+
- lib/utilities/datetime.rb
|
30
|
+
- lib/utilities/enumerable.rb
|
31
|
+
- lib/utilities/hash.rb
|
32
|
+
- lib/utilities/kernel.rb
|
33
|
+
- lib/utilities/numeric.rb
|
34
|
+
- lib/utilities/object.rb
|
35
|
+
- lib/utilities/range.rb
|
36
|
+
- lib/utilities/string.rb
|
37
|
+
- lib/utilities/time.rb
|
38
|
+
- lib/utilities/utilities.rb
|
29
39
|
- lib/version.rb
|
30
40
|
- test/test_utilities.rb
|
41
|
+
- utilities-0.0.14.gem
|
42
|
+
- utilities.gemspec
|
31
43
|
homepage: http://github.com/christianblais/utilities
|
32
44
|
licenses: []
|
33
45
|
post_install_message:
|