utilities 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/utilities.rb +40 -30
- data/lib/version.rb +1 -1
- data/test/test_utilities.rb +63 -0
- data/utilities.gemspec +4 -3
- metadata +7 -8
data/lib/utilities.rb
CHANGED
@@ -48,24 +48,9 @@ class String
|
|
48
48
|
def to_time
|
49
49
|
Time.parse(self)
|
50
50
|
end
|
51
|
-
|
52
|
-
# Transform self to a float representation of a time (HH:MM)
|
53
|
-
# Ex: "14:30" => 14.5, "12h00" => 12.0
|
54
|
-
def hour_to_float
|
55
|
-
hour, min = split(/:|h/)
|
56
|
-
hour.to_f + min.to_f / 60.0
|
57
|
-
end
|
58
51
|
end
|
59
52
|
|
60
53
|
class Numeric
|
61
|
-
# Transform self to a string formatted time (HH:MM)
|
62
|
-
# Ex: 14.5 => "14:30"
|
63
|
-
def hour_to_string delimiter = ':'
|
64
|
-
hour = self.to_i
|
65
|
-
min = "%02d" % (self.abs * 60 % 60).to_i
|
66
|
-
"#{hour}#{delimiter}#{min}"
|
67
|
-
end
|
68
|
-
|
69
54
|
# Convert to degrees
|
70
55
|
def degrees
|
71
56
|
self * Math::PI / 180
|
@@ -76,6 +61,13 @@ class Numeric
|
|
76
61
|
self * self
|
77
62
|
end
|
78
63
|
|
64
|
+
#Transform self to a string formatted time (HH:MM) Ex: 14.5 => “14:30“
|
65
|
+
def hour_to_string delimiter = ':'
|
66
|
+
hour = self.to_i
|
67
|
+
min = "%02d" % (self.abs * 60 % 60).to_i
|
68
|
+
"#{hour}#{delimiter}#{min}"
|
69
|
+
end
|
70
|
+
|
79
71
|
# Return the square root of self
|
80
72
|
def sqrt
|
81
73
|
Math.sqrt(self)
|
@@ -100,10 +92,18 @@ class Numeric
|
|
100
92
|
end
|
101
93
|
|
102
94
|
module Utilities
|
103
|
-
module
|
104
|
-
# Find every
|
95
|
+
module Submodules
|
96
|
+
# Find every submodules of an object.
|
97
|
+
def submodules
|
98
|
+
constants.collect{ |const_name| const_get(const_name) }.select{ |const| const.class == Module }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
module Subclasses
|
103
|
+
# Find every subclass of an object.
|
105
104
|
def subclasses(direct = false)
|
106
105
|
classes = []
|
106
|
+
|
107
107
|
if direct
|
108
108
|
ObjectSpace.each_object(Class) do |c|
|
109
109
|
classes << c if c.superclass == self
|
@@ -113,21 +113,15 @@ module Utilities
|
|
113
113
|
classes << c if c.ancestors.include?(self) and (c != self)
|
114
114
|
end
|
115
115
|
end
|
116
|
+
|
116
117
|
classes
|
117
118
|
end
|
118
119
|
end
|
119
|
-
|
120
|
-
module Submodules
|
121
|
-
# Find every submodules of an object
|
122
|
-
def submodules
|
123
|
-
constants.collect{ |const_name| const_get(const_name) }.select{ |const| const.class == Module }
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
120
|
+
|
127
121
|
module Statistics
|
128
122
|
# Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
|
129
123
|
def sum
|
130
|
-
inject( :+ )
|
124
|
+
flatten.compact.inject( :+ )
|
131
125
|
end
|
132
126
|
|
133
127
|
# Calculate squares of each item
|
@@ -149,7 +143,8 @@ module Utilities
|
|
149
143
|
|
150
144
|
# Calculate the mean of the array, as long as all objects respond to / operator
|
151
145
|
def mean
|
152
|
-
|
146
|
+
a = flatten.compact.to_stat
|
147
|
+
(a.size > 0) ? a.sum.to_f / a.size : 0.0
|
153
148
|
end
|
154
149
|
alias_method :average, :mean
|
155
150
|
|
@@ -183,7 +178,7 @@ module Utilities
|
|
183
178
|
|
184
179
|
# Return the first quartile of self
|
185
180
|
def first_quartile( already_sorted = false )
|
186
|
-
return nil
|
181
|
+
return nil unless size >= 4
|
187
182
|
a = already_sorted ? self : sort
|
188
183
|
a[0..((size / 2) - 1)].extend(Utilities::Statistics).median( true )
|
189
184
|
end
|
@@ -191,7 +186,7 @@ module Utilities
|
|
191
186
|
|
192
187
|
# Return the last quartile of self
|
193
188
|
def last_quartile( already_sorted = false )
|
194
|
-
return nil
|
189
|
+
return nil unless size >= 4
|
195
190
|
a = already_sorted ? self : sort
|
196
191
|
a[((size / 2) + 1)..-1].extend(Utilities::Statistics).median( true )
|
197
192
|
end
|
@@ -205,7 +200,7 @@ module Utilities
|
|
205
200
|
|
206
201
|
# Calculate the interquartile range of self
|
207
202
|
def interquartile_range( already_sorted = false )
|
208
|
-
return nil
|
203
|
+
return nil unless size >= 4
|
209
204
|
a = sort_and_extend( already_sorted )
|
210
205
|
a.last_quartile - a.first_quartile
|
211
206
|
end
|
@@ -267,6 +262,19 @@ module Utilities
|
|
267
262
|
end
|
268
263
|
|
269
264
|
class Array
|
265
|
+
# Returns true if the array contains only numerical values
|
266
|
+
def numerics?( allow_nil = false )
|
267
|
+
(allow_nil ? compact : self).reject{ |x| x.is_a?( Numeric ) }.empty?
|
268
|
+
end
|
269
|
+
alias_method :numeric?, :numerics?
|
270
|
+
alias_method :narray?, :numerics?
|
271
|
+
|
272
|
+
# Transforms an array
|
273
|
+
def to_numerics
|
274
|
+
map{ |x| x.to_f }
|
275
|
+
end
|
276
|
+
alias_method :to_numeric, :to_numerics
|
277
|
+
alias_method :to_narray, :to_numerics
|
270
278
|
|
271
279
|
# Returns a copy of self reverse sorted
|
272
280
|
def reverse_sort
|
@@ -284,9 +292,11 @@ class Array
|
|
284
292
|
def to_stat
|
285
293
|
dup.to_stat!
|
286
294
|
end
|
295
|
+
alias_method :to_stats, :to_stat
|
287
296
|
|
288
297
|
# Adds the statistics methods to self
|
289
298
|
def to_stat!
|
290
299
|
extend(Utilities::Statistics)
|
291
300
|
end
|
301
|
+
alias_method :to_stats!, :to_stat!
|
292
302
|
end
|
data/lib/version.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
require '../lib/utilities'
|
2
|
+
|
3
|
+
describe Range do
|
4
|
+
it "#intersection should returns a range containing elements common to the two ranges, with no duplicates" do
|
5
|
+
(1..10).intersection(5..15).should == (5..10)
|
6
|
+
(1..10).intersection(15..25).should == nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it "#empty? should returns true if given Range is empty" do
|
10
|
+
# Can a range be empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
it "#overlap? should returns true if the given Range overlaps the other" do
|
14
|
+
(1..5).overlap?(10..20).should == false
|
15
|
+
(1..5).overlap?(4..9).should == true
|
16
|
+
(1..5).overlap?(5..10).should == true
|
17
|
+
(1...5).overlap?(5..10).should == false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Numeric do
|
22
|
+
it "#degrees should returns the right degrees for any given Numeric" do
|
23
|
+
0.degrees.should == 0
|
24
|
+
90.degrees.should == Math::PI / 2
|
25
|
+
180.degrees.should == Math::PI
|
26
|
+
360.degrees.should == Math::PI * 2
|
27
|
+
end
|
28
|
+
|
29
|
+
it "#square should returns the square of any given Numeric (based on Numeric ** method)" do
|
30
|
+
(-5..5).each do |i|
|
31
|
+
i.square.should == i**2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "#sqrt should returns the square root of any given Numeric (based on Math::sqrt)" do
|
36
|
+
(1..25).each do |i|
|
37
|
+
i.sqrt.should == Math::sqrt(i)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "#rank should returns the rank of any given Numeric" do
|
42
|
+
1.rank(1, 5).should == (1.0 - 1.0) / (5.0 - 1.0)
|
43
|
+
5.rank(3, 9).should == (5.0 - 3.0) / (9.0 - 3.0)
|
44
|
+
-4.rank(1, 5).should == (-4.0 - 1.0) / (5.0 - 1.0)
|
45
|
+
1.rank(1, 1).should == 0.0
|
46
|
+
end
|
47
|
+
|
48
|
+
it "#to_decimals should return a string of format %.nf" do
|
49
|
+
1.to_decimals(1).should == "1.0"
|
50
|
+
1.to_decimals(4).should == "1.0000"
|
51
|
+
1.983.to_decimals.should == "1.98"
|
52
|
+
1.987.to_decimals.should == "1.99"
|
53
|
+
-1.531.to_decimals.should == "-1.53"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "#percentage_of should return the percent of any given Numeric on n" do
|
57
|
+
1.percentage_of(100).should == 1
|
58
|
+
1.percentage_of(50).should == 2
|
59
|
+
1.percentage_of(1).should == 100
|
60
|
+
1.percentage_of(50, 50).should == 1
|
61
|
+
100.percentage_of(50).should == 200
|
62
|
+
end
|
63
|
+
end
|
data/utilities.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
lib = File.expand_path('../lib/', __FILE__)
|
3
3
|
$:.unshift lib unless $:.include?(lib)
|
4
4
|
|
5
|
-
require 'lib/version'
|
5
|
+
require './lib/version'
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "utilities"
|
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
'README',
|
20
20
|
'utilities.gemspec',
|
21
21
|
'lib/utilities.rb',
|
22
|
-
'lib/version.rb'
|
22
|
+
'lib/version.rb',
|
23
|
+
'test/test_utilities.rb']
|
23
24
|
|
24
|
-
s.require_paths = ['lib']
|
25
|
+
s.require_paths = ['lib', 'test']
|
25
26
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 9
|
9
|
+
version: 0.0.9
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Christian Blais
|
@@ -17,7 +16,7 @@ autorequire:
|
|
17
16
|
bindir: bin
|
18
17
|
cert_chain: []
|
19
18
|
|
20
|
-
date: 2011-
|
19
|
+
date: 2011-06-21 00:00:00 -04:00
|
21
20
|
default_executable:
|
22
21
|
dependencies: []
|
23
22
|
|
@@ -37,6 +36,7 @@ files:
|
|
37
36
|
- utilities.gemspec
|
38
37
|
- lib/utilities.rb
|
39
38
|
- lib/version.rb
|
39
|
+
- test/test_utilities.rb
|
40
40
|
has_rdoc: true
|
41
41
|
homepage: http://github.com/christianblais/utilities
|
42
42
|
licenses: []
|
@@ -46,12 +46,12 @@ rdoc_options: []
|
|
46
46
|
|
47
47
|
require_paths:
|
48
48
|
- lib
|
49
|
+
- test
|
49
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
51
|
none: false
|
51
52
|
requirements:
|
52
53
|
- - ">="
|
53
54
|
- !ruby/object:Gem::Version
|
54
|
-
hash: 3
|
55
55
|
segments:
|
56
56
|
- 0
|
57
57
|
version: "0"
|
@@ -60,14 +60,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
requirements:
|
61
61
|
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
hash: 3
|
64
63
|
segments:
|
65
64
|
- 0
|
66
65
|
version: "0"
|
67
66
|
requirements: []
|
68
67
|
|
69
68
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.
|
69
|
+
rubygems_version: 1.3.7
|
71
70
|
signing_key:
|
72
71
|
specification_version: 3
|
73
72
|
summary: Few utilities include in all my projects
|