utilities 0.0.1 → 0.0.2
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.
- data/lib/core.rb +61 -59
- data/lib/version.rb +1 -1
- data/utilities.gemspec +25 -0
- metadata +7 -6
data/lib/core.rb
CHANGED
@@ -34,12 +34,12 @@ class Time
|
|
34
34
|
end
|
35
35
|
|
36
36
|
class String
|
37
|
-
# Transform
|
37
|
+
# Transform self to a Date
|
38
38
|
def to_date
|
39
39
|
Date.parse(self)
|
40
40
|
end
|
41
41
|
|
42
|
-
# Transform
|
42
|
+
# Transform self to a Time
|
43
43
|
def to_time
|
44
44
|
Time.parse(self)
|
45
45
|
end
|
@@ -63,63 +63,65 @@ class Numeric
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
module
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
66
|
+
module Utilities
|
67
|
+
module Statistics
|
68
|
+
# Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
|
69
|
+
def sum
|
70
|
+
inject( nil ) do |sum, x|
|
71
|
+
sum ? sum + x : x
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Calculate the mean of the array, as long as all objects respond to / operator
|
76
|
+
def mean
|
77
|
+
(size > 0) ? sum.to_f / size : 0.0
|
78
|
+
end
|
79
|
+
alias_method :average, :mean
|
80
|
+
|
81
|
+
# Calculate the number of occurences for each element of the array
|
82
|
+
def frequences
|
83
|
+
inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
84
|
+
end
|
85
|
+
|
86
|
+
# Return the variance of the array
|
87
|
+
def variance
|
88
|
+
m = mean
|
89
|
+
inject(0) { |v, x| v += (x - m) ** 2 }
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return the standard deviation of the array
|
93
|
+
def standard_deviation
|
94
|
+
size > 1 ? Math.sqrt( variance / ( size - 1 ) ) : 0.0
|
95
|
+
end
|
96
|
+
alias_method :std_dev, :standard_deviation
|
97
|
+
|
98
|
+
# Return the median of the array
|
99
|
+
def median(already_sorted=false)
|
100
|
+
return nil if empty?
|
101
|
+
a = sort unless already_sorted
|
102
|
+
m_pos = size / 2
|
103
|
+
size % 2 == 1 ? a[m_pos] : a[m_pos-1..m_pos].mean
|
104
|
+
end
|
105
|
+
|
106
|
+
# Return an array of modes with their corresponding occurences
|
107
|
+
def modes
|
108
|
+
freq = frequences
|
109
|
+
max = freq.values.max
|
110
|
+
freq.select { |k, f| f == max }
|
111
|
+
end
|
112
|
+
|
113
|
+
# Return all statistics from the array in a simple hash
|
114
|
+
def stats
|
115
|
+
{
|
116
|
+
:size => size,
|
117
|
+
:sum => sum,
|
118
|
+
:min => min,
|
119
|
+
:max => max,
|
120
|
+
:mean => mean,
|
121
|
+
:standard_deviation => standard_deviation,
|
122
|
+
:median => median,
|
123
|
+
:modes => modes
|
124
|
+
}
|
71
125
|
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# Calculate the mean of the array, as long as all objects respond to / operator
|
75
|
-
def mean
|
76
|
-
(size > 0) ? sum.to_f / size : 0.0
|
77
|
-
end
|
78
|
-
alias_method :average, :mean
|
79
|
-
|
80
|
-
# Calculate the number of occurences for each element of the array
|
81
|
-
def frequences
|
82
|
-
inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
83
|
-
end
|
84
|
-
|
85
|
-
# Return the variance of the array
|
86
|
-
def variance
|
87
|
-
m = mean
|
88
|
-
inject(0) { |v, x| v += (x - m) ** 2 }
|
89
|
-
end
|
90
|
-
|
91
|
-
# Return the standard_deviation of the array
|
92
|
-
def standard_deviation
|
93
|
-
size > 1 ? Math.sqrt( variance / ( size - 1 ) ) : 0.0
|
94
|
-
end
|
95
|
-
alias_method :std_dev, :standard_deviation
|
96
|
-
|
97
|
-
# Return the median of the array
|
98
|
-
def median(already_sorted=false)
|
99
|
-
return nil if empty?
|
100
|
-
a = sort unless already_sorted
|
101
|
-
m_pos = size / 2
|
102
|
-
size % 2 == 1 ? a[m_pos] : a[m_pos-1..m_pos].mean
|
103
|
-
end
|
104
|
-
|
105
|
-
# Return an array of modes with their corresponding occurences
|
106
|
-
def modes
|
107
|
-
freq = frequences
|
108
|
-
max = freq.values.max
|
109
|
-
freq.select { |k, f| f == max }
|
110
|
-
end
|
111
|
-
|
112
|
-
# Return all statistics from the array in a simple hash
|
113
|
-
def stats
|
114
|
-
{
|
115
|
-
:size => size,
|
116
|
-
:sum => sum,
|
117
|
-
:min => min,
|
118
|
-
:max => max,
|
119
|
-
:mean => mean,
|
120
|
-
:standard_deviation => standard_deviation,
|
121
|
-
:median => median,
|
122
|
-
:modes => modes
|
123
|
-
}
|
124
126
|
end
|
125
127
|
end
|
data/lib/version.rb
CHANGED
data/utilities.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require 'lib/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "utilities"
|
9
|
+
s.version = Utilities::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["Christian Blais"]
|
12
|
+
s.email = ["christ.blais@gmail.com"]
|
13
|
+
s.homepage = "http://github.com/christianblais/utilities"
|
14
|
+
s.summary = "Few utilities include in all my projects"
|
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
|
+
|
17
|
+
s.files = [
|
18
|
+
'LICENSE',
|
19
|
+
'README',
|
20
|
+
'utilities.gemspec',
|
21
|
+
'lib/core.rb',
|
22
|
+
'lib/version.rb']
|
23
|
+
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Blais
|
@@ -19,7 +19,7 @@ date: 2011-02-15 00:00:00 -05:00
|
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description: Few utilities include in all my projects, including a module for statistics, some to_date and to_time
|
22
|
+
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.
|
23
23
|
email:
|
24
24
|
- christ.blais@gmail.com
|
25
25
|
executables: []
|
@@ -29,10 +29,11 @@ extensions: []
|
|
29
29
|
extra_rdoc_files: []
|
30
30
|
|
31
31
|
files:
|
32
|
-
- lib/core.rb
|
33
|
-
- lib/version.rb
|
34
32
|
- LICENSE
|
35
33
|
- README
|
34
|
+
- utilities.gemspec
|
35
|
+
- lib/core.rb
|
36
|
+
- lib/version.rb
|
36
37
|
has_rdoc: true
|
37
38
|
homepage: http://github.com/christianblais/utilities
|
38
39
|
licenses: []
|