utilities 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +6 -0
  2. data/README +0 -0
  3. data/lib/core.rb +125 -0
  4. data/lib/version.rb +7 -0
  5. metadata +71 -0
data/LICENSE ADDED
@@ -0,0 +1,6 @@
1
+ COPYLEFT
2
+
3
+ 0. the freedom to use the work,
4
+ 1. the freedom to study the work,
5
+ 2. the freedom to copy and share the work with others,
6
+ 3. the freedom to modify the work, and the freedom to distribute modified and therefore derivative works.
data/README ADDED
File without changes
data/lib/core.rb ADDED
@@ -0,0 +1,125 @@
1
+ module Kernel
2
+ # Add .inspect to any object passed, than call Kernel.raise
3
+ def raiser o
4
+ raise o.inspect
5
+ end
6
+ end
7
+
8
+ class Range
9
+ # Return a range containing elements common to the two ranges, with no duplicates
10
+ def intersection range
11
+ values = self.to_a & range.to_a
12
+ values.empty? ? nil : (values.first..values.last)
13
+ end
14
+ alias_method :&, :intersection
15
+
16
+ # Detect if the two ranges overlap one with the other
17
+ def overlap? range
18
+ !(self & range).count.zero?
19
+ end
20
+ end
21
+
22
+ class DateTime
23
+ # Transform a DateTime to a Time
24
+ def to_time
25
+ Time.parse(self.to_s)
26
+ end
27
+ end
28
+
29
+ class Time
30
+ # Transform a Time to a DateTime
31
+ def to_datetime
32
+ DateTime.parse(self.to_s)
33
+ end
34
+ end
35
+
36
+ class String
37
+ # Transform the String to a Date
38
+ def to_date
39
+ Date.parse(self)
40
+ end
41
+
42
+ # Transform the String to a Time
43
+ def to_time
44
+ Time.parse(self)
45
+ end
46
+ end
47
+
48
+ class Numeric
49
+ # Calculate the rank of self based on provided min and max
50
+ def rank min, max
51
+ s, min, max = self.to_f, min.to_f, max.to_f
52
+ min == max ? 0.0 : (s - min) / (max - min)
53
+ end
54
+
55
+ # Transforms self to a string with *decimals* decimals
56
+ def to_decimals decimals = 2
57
+ "%.#{decimals}f" % self
58
+ end
59
+
60
+ # Calculate the percentage of self on n
61
+ def percentage_of n, t = 100
62
+ n == 0 ? 0.0 : self / n.to_f * t
63
+ end
64
+ end
65
+
66
+ module Statistics
67
+ # Add each object of the array to each other in order to get the sum, as long as all objects respond to + operator
68
+ def sum
69
+ inject( nil ) do |sum, x|
70
+ sum ? sum + x : x
71
+ 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
+ end
125
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Utilities
2
+ MAJOR = 0
3
+ MINOR = 0
4
+ BUILD = 1
5
+
6
+ VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
7
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utilities
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Christian Blais
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-15 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Few utilities include in all my projects, including a module for statistics, some to_date and to_time function as well as intersection method for Range object.
23
+ email:
24
+ - christ.blais@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/core.rb
33
+ - lib/version.rb
34
+ - LICENSE
35
+ - README
36
+ has_rdoc: true
37
+ homepage: http://github.com/christianblais/utilities
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Few utilities include in all my projects
70
+ test_files: []
71
+