utilities 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/utilities.rb +11 -360
- data/lib/version.rb +1 -1
- metadata +3 -3
data/lib/utilities.rb
CHANGED
@@ -1,360 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
else
|
13
|
-
enumerable.min <= self && self <= enumerable.max
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class Range
|
19
|
-
# Return a range containing elements common to the two ranges, with no duplicates
|
20
|
-
def intersection range
|
21
|
-
values = self.to_a & range.to_a
|
22
|
-
values.empty? ? nil : (values.first..values.last)
|
23
|
-
end
|
24
|
-
alias_method :&, :intersection
|
25
|
-
|
26
|
-
# Verify if self is empty
|
27
|
-
def empty?
|
28
|
-
count.zero?
|
29
|
-
end
|
30
|
-
|
31
|
-
# Detect if the two ranges overlap one with the other
|
32
|
-
def overlap? range
|
33
|
-
!(self & range).nil?
|
34
|
-
end
|
35
|
-
|
36
|
-
# Adds cover? if not defined (like in previous rubies)
|
37
|
-
unless self.instance_methods.include?(:cover?)
|
38
|
-
def cover? object
|
39
|
-
ends = [self.first, self.last]
|
40
|
-
ends.min <= object && object <= ends.max
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
class DateTime
|
46
|
-
# Transform a DateTime to a Time
|
47
|
-
def to_time
|
48
|
-
Time.parse(self.to_s)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class Time
|
53
|
-
# Transform a Time to a DateTime
|
54
|
-
def to_datetime
|
55
|
-
DateTime.parse(self.to_s)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class String
|
60
|
-
# Transform self to a Date
|
61
|
-
def to_date
|
62
|
-
Date.parse(self)
|
63
|
-
end
|
64
|
-
|
65
|
-
# Transform self to a Time
|
66
|
-
def to_time
|
67
|
-
Time.parse(self)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Numeric
|
72
|
-
# Convert to degrees
|
73
|
-
def degrees
|
74
|
-
self * Math::PI / 180
|
75
|
-
end
|
76
|
-
|
77
|
-
# Return the square of self
|
78
|
-
def square
|
79
|
-
self * self
|
80
|
-
end
|
81
|
-
|
82
|
-
#Transform self to a string formatted time (HH:MM) Ex: 14.5 => “14:30“
|
83
|
-
def hour_to_string delimiter = ':'
|
84
|
-
hour = self.to_i
|
85
|
-
min = "%02d" % (self.abs * 60 % 60).to_i
|
86
|
-
"#{hour}#{delimiter}#{min}"
|
87
|
-
end
|
88
|
-
|
89
|
-
# Return the square root of self
|
90
|
-
def sqrt
|
91
|
-
Math.sqrt(self)
|
92
|
-
end
|
93
|
-
|
94
|
-
# Calculate the rank of self based on provided min and max
|
95
|
-
def rank min, max
|
96
|
-
s, min, max = self.to_f, min.to_f, max.to_f
|
97
|
-
min == max ? 0.0 : (s - min) / (max - min)
|
98
|
-
end
|
99
|
-
|
100
|
-
# Transforms self to a string with *decimals* decimals
|
101
|
-
def to_decimals decimals = 2
|
102
|
-
"%.#{decimals}f" % self
|
103
|
-
end
|
104
|
-
|
105
|
-
# Calculate the percentage of self on n
|
106
|
-
def percentage_of n, t = 100
|
107
|
-
n == 0 ? 0.0 : self / n.to_f * t
|
108
|
-
end
|
109
|
-
alias_method :percent_of, :percentage_of
|
110
|
-
end
|
111
|
-
|
112
|
-
module Utilities
|
113
|
-
module Submodules
|
114
|
-
# Find every submodules of an object.
|
115
|
-
def submodules
|
116
|
-
constants.collect{ |const_name| const_get(const_name) }.select{ |const| const.class == Module }
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
module Subclasses
|
121
|
-
# Find every subclass of an object.
|
122
|
-
def subclasses(direct = false)
|
123
|
-
classes = []
|
124
|
-
|
125
|
-
if direct
|
126
|
-
ObjectSpace.each_object(Class) do |c|
|
127
|
-
classes << c if c.superclass == self
|
128
|
-
end
|
129
|
-
else
|
130
|
-
ObjectSpace.each_object(Class) do |c|
|
131
|
-
classes << c if c.ancestors.include?(self) and (c != self)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
classes
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
module Statistics
|
140
|
-
# Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
|
141
|
-
def sum
|
142
|
-
flatten.compact.inject( :+ )
|
143
|
-
end
|
144
|
-
|
145
|
-
# Calculate squares of each item
|
146
|
-
def squares
|
147
|
-
map{ |i| i**2 }
|
148
|
-
end
|
149
|
-
|
150
|
-
# Return a new array containing the rank of each value
|
151
|
-
# Ex: [1, 2, 2, 8, 9] #=> [0.0, 1.5, 1.5, 3.0, 4.0]
|
152
|
-
def ranks( already_sorted = false )
|
153
|
-
a = already_sorted ? self : sort
|
154
|
-
map{ |i| (a.index(i) + a.rindex(i)) / 2.0 }
|
155
|
-
end
|
156
|
-
|
157
|
-
# Calculate square roots of each item
|
158
|
-
def sqrts
|
159
|
-
map{ |i| i.sqrt }
|
160
|
-
end
|
161
|
-
|
162
|
-
# Calculate the mean of the array, as long as all objects respond to / operator
|
163
|
-
def mean
|
164
|
-
a = flatten.compact.to_stat
|
165
|
-
(a.size > 0) ? a.sum.to_f / a.size : 0.0
|
166
|
-
end
|
167
|
-
alias_method :average, :mean
|
168
|
-
|
169
|
-
# Calculate the number of occurences for each element of the array
|
170
|
-
def frequences
|
171
|
-
inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
172
|
-
end
|
173
|
-
|
174
|
-
# Return the variance of self
|
175
|
-
def variance
|
176
|
-
m = mean
|
177
|
-
inject(0) { |v, x| v += (x - m) ** 2 }
|
178
|
-
end
|
179
|
-
|
180
|
-
# Return the (sample|population) standard deviation of self
|
181
|
-
# If population is set to true, then we consider the dataset as the complete population
|
182
|
-
# Else, we consider the dataset as a sample, so we use the sample standard deviation (size - 1)
|
183
|
-
def standard_deviation( population = false )
|
184
|
-
size > 1 ? Math.sqrt( variance / ( size - ( population ? 0 : 1 ) ) ) : 0.0
|
185
|
-
end
|
186
|
-
alias_method :std_dev, :standard_deviation
|
187
|
-
|
188
|
-
# Return the median of sorted self
|
189
|
-
def median( already_sorted = false )
|
190
|
-
return nil if empty?
|
191
|
-
a = sort_and_extend( already_sorted )
|
192
|
-
m_pos = size / 2
|
193
|
-
size % 2 == 1 ? a[m_pos] : (a[m_pos-1] + a[m_pos]).to_f / 2
|
194
|
-
end
|
195
|
-
alias_method :second_quartile, :median
|
196
|
-
|
197
|
-
# Return the first quartile of self
|
198
|
-
def first_quartile( already_sorted = false )
|
199
|
-
return nil if size < 4
|
200
|
-
a = already_sorted ? self : sort
|
201
|
-
a[0..((size / 2) - 1)].extend(Utilities::Statistics).median( true )
|
202
|
-
end
|
203
|
-
alias_method :lower_quartile, :first_quartile
|
204
|
-
|
205
|
-
# Return the last quartile of self
|
206
|
-
def last_quartile( already_sorted = false )
|
207
|
-
return nil if size < 4
|
208
|
-
a = already_sorted ? self : sort
|
209
|
-
a[((size / 2) + 1)..-1].extend(Utilities::Statistics).median( true )
|
210
|
-
end
|
211
|
-
alias_method :upper_quartile, :last_quartile
|
212
|
-
|
213
|
-
# Return an array containing the first, the second and the last quartile of self
|
214
|
-
def quartiles( already_sorted = false )
|
215
|
-
a = sort_and_extend( already_sorted )
|
216
|
-
[a.first_quartile( true ), a.median( true ), a.last_quartile( true )]
|
217
|
-
end
|
218
|
-
|
219
|
-
# Calculate the interquartile range of self
|
220
|
-
def interquartile_range( already_sorted = false )
|
221
|
-
return nil if size < 4
|
222
|
-
a = sort_and_extend( already_sorted )
|
223
|
-
a.last_quartile - a.first_quartile
|
224
|
-
end
|
225
|
-
|
226
|
-
# Return an array of modes with their corresponding occurences
|
227
|
-
def modes
|
228
|
-
freq = frequences
|
229
|
-
max = freq.values.max
|
230
|
-
freq.select { |k, f| f == max }
|
231
|
-
end
|
232
|
-
|
233
|
-
# Return the midrange of sorted self
|
234
|
-
def midrange( already_sorted = false )
|
235
|
-
return nil if empty?
|
236
|
-
a = sort_and_extend( already_sorted )
|
237
|
-
(a.first + a.last) / 2.0
|
238
|
-
end
|
239
|
-
|
240
|
-
# Return the statistical range of sorted self
|
241
|
-
def statistical_range( already_sorted = false )
|
242
|
-
return nil if empty?
|
243
|
-
a = sort_and_extend( already_sorted )
|
244
|
-
(a.last - a.first)
|
245
|
-
end
|
246
|
-
|
247
|
-
# Return all statistics from self in a simple hash
|
248
|
-
def statistics( already_sorted = false )
|
249
|
-
sorted = sort_and_extend( already_sorted )
|
250
|
-
|
251
|
-
{
|
252
|
-
:first => self.first,
|
253
|
-
:last => self.last,
|
254
|
-
:size => self.size,
|
255
|
-
:sum => self.sum,
|
256
|
-
:min => self.min,
|
257
|
-
:max => self.max,
|
258
|
-
:mean => self.mean,
|
259
|
-
:frequences => self.frequences,
|
260
|
-
:variance => self.variance,
|
261
|
-
:standard_deviation => self.standard_deviation,
|
262
|
-
:modes => self.modes,
|
263
|
-
|
264
|
-
# Need to be sorted...
|
265
|
-
:ranks => sorted.ranks( true ),
|
266
|
-
:median => sorted.median( true ),
|
267
|
-
:midrange => sorted.midrange( true ),
|
268
|
-
:statistical_range => sorted.statistical_range( true ),
|
269
|
-
:quartiles => sorted.quartiles( true ),
|
270
|
-
:interquartile_range => sorted.interquartile_range( true )
|
271
|
-
}
|
272
|
-
end
|
273
|
-
alias_method :stats, :statistics
|
274
|
-
|
275
|
-
protected
|
276
|
-
def sort_and_extend( already_sorted = false )
|
277
|
-
already_sorted ? self : sort.extend(Utilities::Statistics)
|
278
|
-
end
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
class Hash
|
283
|
-
# Returns a new hash with the results of running block once for every key in self
|
284
|
-
def collect_keys
|
285
|
-
each_with_object({}){ |(k,v),h| h[yield(k)] = v } if block_given?
|
286
|
-
end
|
287
|
-
alias_method :map_keys, :collect_keys
|
288
|
-
|
289
|
-
# Returns a new hash with the results of running block once for every value in self
|
290
|
-
def collect_values
|
291
|
-
each_with_object({}){ |(k,v),h| h[k] = yield(v) } if block_given?
|
292
|
-
end
|
293
|
-
alias_method :map_values, :collect_values
|
294
|
-
|
295
|
-
# Returns a new hash where all keys have been symbolized
|
296
|
-
def symbolize_keys
|
297
|
-
collect_keys{ |k| k.to_sym }
|
298
|
-
end
|
299
|
-
end
|
300
|
-
|
301
|
-
class Array
|
302
|
-
# Returns true if the array contains only numerical values
|
303
|
-
def numerics?( allow_nil = false )
|
304
|
-
(allow_nil ? compact : self).reject{ |x| x.is_a?( Numeric ) }.empty?
|
305
|
-
end
|
306
|
-
alias_method :numeric?, :numerics?
|
307
|
-
alias_method :narray?, :numerics?
|
308
|
-
|
309
|
-
# Transforms an array
|
310
|
-
def to_numerics
|
311
|
-
map{ |x| x.to_f }
|
312
|
-
end
|
313
|
-
alias_method :to_numeric, :to_numerics
|
314
|
-
alias_method :to_narray, :to_numerics
|
315
|
-
|
316
|
-
# Returns a copy of self reverse sorted
|
317
|
-
def reverse_sort
|
318
|
-
dup.rsort!
|
319
|
-
end
|
320
|
-
alias_method :rsort, :reverse_sort
|
321
|
-
|
322
|
-
# Reverse sort self
|
323
|
-
def reverse_sort!
|
324
|
-
sort!{|x,y| y <=> x }
|
325
|
-
end
|
326
|
-
alias_method :rsort!, :reverse_sort!
|
327
|
-
|
328
|
-
# Returns a copy of self that includes the Statistics methods
|
329
|
-
def to_stat
|
330
|
-
dup.to_stat!
|
331
|
-
end
|
332
|
-
alias_method :to_stats, :to_stat
|
333
|
-
|
334
|
-
# Adds the statistics methods to self
|
335
|
-
def to_stat!
|
336
|
-
extend(Utilities::Statistics)
|
337
|
-
end
|
338
|
-
alias_method :to_stats!, :to_stat!
|
339
|
-
end
|
340
|
-
|
341
|
-
|
342
|
-
module Enumerable
|
343
|
-
# Collects the first N truthy values
|
344
|
-
# {}.collect_first {|a, b| obj} => obj
|
345
|
-
# {}.collect_first(n) {|a, b| obj} => [obj]
|
346
|
-
def collect_first( amount = nil, &block )
|
347
|
-
array = !amount.nil?
|
348
|
-
amount ||= 1
|
349
|
-
|
350
|
-
values = []
|
351
|
-
self.each do |val|
|
352
|
-
break if values.length >= amount
|
353
|
-
t = yield(val)
|
354
|
-
values << t if t
|
355
|
-
end
|
356
|
-
|
357
|
-
!array && amount == 1 ? values.first : values
|
358
|
-
end
|
359
|
-
alias_method :map_first, :collect_first
|
360
|
-
end
|
1
|
+
require File.dirname(__FILE__) + "/utilities/array"
|
2
|
+
require File.dirname(__FILE__) + "/utilities/datetime"
|
3
|
+
require File.dirname(__FILE__) + "/utilities/enumerable"
|
4
|
+
require File.dirname(__FILE__) + "/utilities/hash"
|
5
|
+
require File.dirname(__FILE__) + "/utilities/kernel"
|
6
|
+
require File.dirname(__FILE__) + "/utilities/numeric"
|
7
|
+
require File.dirname(__FILE__) + "/utilities/object"
|
8
|
+
require File.dirname(__FILE__) + "/utilities/range"
|
9
|
+
require File.dirname(__FILE__) + "/utilities/string"
|
10
|
+
require File.dirname(__FILE__) + "/utilities/time"
|
11
|
+
require File.dirname(__FILE__) + "/utilities/utilities"
|
data/lib/version.rb
CHANGED
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.14
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-08-
|
14
|
+
date: 2011-08-28 00:00:00.000000000Z
|
15
15
|
dependencies: []
|
16
16
|
description: Few utilities include in all my projects, including a module for statistics,
|
17
17
|
some to_date and to_time functions as well as intersection method for Range object.
|
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
49
|
version: '0'
|
50
50
|
requirements: []
|
51
51
|
rubyforge_project:
|
52
|
-
rubygems_version: 1.8.
|
52
|
+
rubygems_version: 1.8.8
|
53
53
|
signing_key:
|
54
54
|
specification_version: 3
|
55
55
|
summary: Few utilities include in all my projects
|