utilities 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +109 -0
- data/lib/utilities/array.rb +3 -3
- data/lib/utilities/utilities.rb +2 -2
- data/lib/version.rb +1 -1
- data/test/test_utilities.rb +68 -27
- data/utilities-0.0.15.gem +0 -0
- metadata +4 -4
- data/README +0 -0
- data/utilities-0.0.14.gem +0 -0
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
Utilities
|
2
|
+
=========
|
3
|
+
Utilities provides a handful of useful goodies including statistics, numeric arrays and the well-known raiser.
|
4
|
+
|
5
|
+
Installation
|
6
|
+
------------
|
7
|
+
gem install utilities
|
8
|
+
|
9
|
+
Test
|
10
|
+
----
|
11
|
+
rspec test/test_utilities
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
#### Array
|
16
|
+
* to_numerics
|
17
|
+
|
18
|
+
["3", 4, "7.9"].to_numerics #=> [3.0, 4.0, 7.9]
|
19
|
+
|
20
|
+
* numerics?
|
21
|
+
|
22
|
+
[1, 2.0, 3].numerics? #=> true
|
23
|
+
|
24
|
+
* reverse_sort
|
25
|
+
|
26
|
+
[1,8,3].reverse_sort #=> [8,3,1]
|
27
|
+
|
28
|
+
#### Enumerable
|
29
|
+
* collect_first
|
30
|
+
|
31
|
+
{:a=>1, :b=>2, :c=>3}.collect_first{|k,v| [k, v * 3] if v == 2 } #=> [:b, 6]
|
32
|
+
|
33
|
+
#### Hash
|
34
|
+
* collect_keys
|
35
|
+
|
36
|
+
{:a=>1, :b=>2, :c=>3}.collect_keys{|k| k.to_s.upcase } #=> {"A"=>1, "B"=>2, "C"=>3}
|
37
|
+
|
38
|
+
* collect_values
|
39
|
+
|
40
|
+
{:a=>1, :b=>2, :c=>3}.collect_values{|v| v * -1 } #=> {:a=>-1, :b=>-2, :c=>-3}
|
41
|
+
|
42
|
+
* symbolize_keys
|
43
|
+
|
44
|
+
{"a"=>1, "b"=>2}.symbolize_keys #=> {:a=>1, :b=>2}
|
45
|
+
|
46
|
+
#### Kernel
|
47
|
+
* raiser
|
48
|
+
|
49
|
+
raiser 1, Hash.new, Array.new #=> RuntimeError "1, {}, []"
|
50
|
+
|
51
|
+
#### Numeric
|
52
|
+
* degrees
|
53
|
+
|
54
|
+
180.degrees == Math::PI #=> true
|
55
|
+
|
56
|
+
* square
|
57
|
+
|
58
|
+
2.square #=> 4
|
59
|
+
|
60
|
+
* hour_to_string
|
61
|
+
|
62
|
+
14.5.hour_to_string #=> "14:30"
|
63
|
+
|
64
|
+
* sqrt
|
65
|
+
|
66
|
+
9.sqrt #=> 3
|
67
|
+
|
68
|
+
* rank
|
69
|
+
|
70
|
+
5.rank(3, 9) #=> 0.3333...
|
71
|
+
|
72
|
+
* to_decimals
|
73
|
+
|
74
|
+
1.759.to_decimals(2) #=> 1.76
|
75
|
+
|
76
|
+
* percentage_of
|
77
|
+
|
78
|
+
48.percentage_of(50) #=> 96
|
79
|
+
|
80
|
+
##### Utilities
|
81
|
+
* Statistics
|
82
|
+
|
83
|
+
This module is intended to provide basic statistic functionnalities to numeric arrays. You may either
|
84
|
+
call [].to_stats or extend an existing array with Utilities::Statistics module.
|
85
|
+
|
86
|
+
** sum
|
87
|
+
|
88
|
+
[1,2,3].sum #=> 6
|
89
|
+
|
90
|
+
** squares
|
91
|
+
|
92
|
+
[1,2,3].squares #=> [1,4,9]
|
93
|
+
|
94
|
+
** sqrts
|
95
|
+
|
96
|
+
[9,16,25].sqrts #=> [3,4,5]
|
97
|
+
|
98
|
+
** mean
|
99
|
+
|
100
|
+
[1,2,3,4,5].mean #=> 3
|
101
|
+
|
102
|
+
** frequences
|
103
|
+
|
104
|
+
[1,1,2,3,3,3,4].frequences #=> {1=>2, 2=>1, 3=>3, 4=>1}
|
105
|
+
|
106
|
+
** modes
|
107
|
+
|
108
|
+
[1,2,3,3,4,4,4,5].modes #=> {4=>3}
|
109
|
+
|
data/lib/utilities/array.rb
CHANGED
@@ -6,9 +6,9 @@ class Array
|
|
6
6
|
alias_method :numeric?, :numerics?
|
7
7
|
alias_method :narray?, :numerics?
|
8
8
|
|
9
|
-
# Transforms an array
|
10
|
-
def to_numerics
|
11
|
-
map{ |x| x.to_f }
|
9
|
+
# Transforms an array to an array of float values
|
10
|
+
def to_numerics( allow_nil = false )
|
11
|
+
map{ |x| allow_nil && x.nil? ? nil : x.to_f }
|
12
12
|
end
|
13
13
|
alias_method :to_numeric, :to_numerics
|
14
14
|
alias_method :to_narray, :to_numerics
|
data/lib/utilities/utilities.rb
CHANGED
@@ -112,10 +112,10 @@ module Utilities
|
|
112
112
|
a.last_quartile - a.first_quartile
|
113
113
|
end
|
114
114
|
|
115
|
-
# Return
|
115
|
+
# Return a hash of modes with their corresponding occurences
|
116
116
|
def modes
|
117
117
|
fre = frequences
|
118
|
-
max =
|
118
|
+
max = fre.values.max
|
119
119
|
fre.select{ |k, f| f == max }
|
120
120
|
end
|
121
121
|
|
data/lib/version.rb
CHANGED
data/test/test_utilities.rb
CHANGED
@@ -1,20 +1,59 @@
|
|
1
|
+
require "date"
|
1
2
|
require File.join(File.dirname(__FILE__), "..", "lib", "utilities")
|
2
3
|
|
3
|
-
describe
|
4
|
-
it "#
|
5
|
-
|
6
|
-
|
4
|
+
describe Array do
|
5
|
+
it "#numerics? should returns true if given Array contains only numeric values" do
|
6
|
+
[].numerics?.should == true
|
7
|
+
[1,2,3,4,5].numerics?.should == true
|
8
|
+
[1,2,3,4,nil].numerics?.should == false
|
9
|
+
[1,2,3,4,nil].numerics?(true).should == true
|
7
10
|
end
|
8
11
|
|
9
|
-
it "#
|
10
|
-
|
12
|
+
it "#to_numerics should returns a new array with only numeric elements" do
|
13
|
+
[].to_numerics.should == []
|
14
|
+
["3", 4, nil].to_numerics.should == [3.0, 4.0, 0.0]
|
15
|
+
["3", 4, nil].to_numerics(true).should == [3.0, 4.0, nil]
|
11
16
|
end
|
12
17
|
|
13
|
-
it "#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
it "#reverse_sort should reverse sort the given array" do
|
19
|
+
[1,2,3,4].reverse_sort.should == [4,3,2,1]
|
20
|
+
[4,3,2,1].reverse_sort.should == [4,3,2,1]
|
21
|
+
["f", "3", "z"].reverse_sort.should == ["z", "f", "3"]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "#to_stat should extends given array with module Utilities::Statistics" do
|
25
|
+
[].methods.include?(:statistics).should == false
|
26
|
+
[].to_stat.methods.include?(:statistics).should == true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe Enumerable do
|
31
|
+
it "#collect_first should return the first collected elements" do
|
32
|
+
h = {:a => 1, :b => 2, :c => 3}
|
33
|
+
h.collect_first{|k, v| v if v == 2}.should == 2
|
34
|
+
h.collect_first(1){|k, v| v}.should == [1]
|
35
|
+
h.collect_first(2){|k, v| v}.should == [1, 2]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe Hash do
|
40
|
+
it "#collect_keys should returns a new hash with the results of running block once for every key in self" do
|
41
|
+
{"a"=>1, "b"=>2, "c"=>3}.collect_keys{|k| k + "z"}.should == {"az"=>1, "bz"=>2, "cz"=>3}
|
42
|
+
end
|
43
|
+
|
44
|
+
it "#collect_values should returns a new hash with the results of running block once for every value in self" do
|
45
|
+
{"a"=>1, "b"=>2, "c"=>3}.collect_values{|v| v**2}.should == {"a"=>1, "b"=>4, "c"=>9}
|
46
|
+
end
|
47
|
+
|
48
|
+
it "#symbolize_keys should returns a new hash where all keys have been symbolized" do
|
49
|
+
{"a"=>1, "b"=>2, "c"=>3}.symbolize_keys.should == {:a=>1, :b=>2, :c=>3}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe Kernel do
|
54
|
+
it "#raiser should raise given object.inspect" do
|
55
|
+
lambda{ raiser(1) }.should raise_error( RuntimeError, '1' )
|
56
|
+
lambda{ raiser([:a, :b], 3, "test") }.should raise_error( RuntimeError, '[:a, :b], 3, "test"' )
|
18
57
|
end
|
19
58
|
end
|
20
59
|
|
@@ -32,6 +71,12 @@ describe Numeric do
|
|
32
71
|
end
|
33
72
|
end
|
34
73
|
|
74
|
+
it "#hour_to_string should returns a formatted string of format HH:MM" do
|
75
|
+
14.5.hour_to_string.should == "14:30"
|
76
|
+
3.2.hour_to_string('h').should == "3h12"
|
77
|
+
19.hour_to_string.should == "19:00"
|
78
|
+
end
|
79
|
+
|
35
80
|
it "#sqrt should returns the square root of any given Numeric (based on Math::sqrt)" do
|
36
81
|
(1..25).each do |i|
|
37
82
|
i.sqrt.should == Math::sqrt(i)
|
@@ -58,29 +103,25 @@ describe Numeric do
|
|
58
103
|
1.percentage_of(50).should == 2
|
59
104
|
1.percentage_of(1).should == 100
|
60
105
|
1.percentage_of(50, 50).should == 1
|
106
|
+
18.percentage_of(20, 5).should == 4.5
|
61
107
|
100.percentage_of(50).should == 200
|
62
108
|
end
|
63
109
|
end
|
64
110
|
|
65
|
-
describe
|
66
|
-
it "#
|
67
|
-
|
111
|
+
describe Range do
|
112
|
+
it "#intersection should returns a range containing elements common to the two ranges, with no duplicates" do
|
113
|
+
(1..10).intersection(5..15).should == (5..10)
|
114
|
+
(1..10).intersection(15..25).should == nil
|
68
115
|
end
|
69
116
|
|
70
|
-
it "#
|
71
|
-
|
117
|
+
it "#empty? should returns true if given Range is empty" do
|
118
|
+
# Can a range be empty?
|
72
119
|
end
|
73
120
|
|
74
|
-
it "#
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
describe Enumerable do
|
80
|
-
it "#collect_first should return the first collected elements" do
|
81
|
-
h = {:a => 1, :b => 2, :c => 3}
|
82
|
-
h.collect_first{|k, v| v if v == 2}.should == 2
|
83
|
-
h.collect_first(1){|k, v| v}.should == [1]
|
84
|
-
h.collect_first(2){|k, v| v}.should == [1, 2]
|
121
|
+
it "#overlap? should returns true if the given Range overlaps the other" do
|
122
|
+
(1..5).overlap?(10..20).should == false
|
123
|
+
(1..5).overlap?(4..9).should == true
|
124
|
+
(1..5).overlap?(5..10).should == true
|
125
|
+
(1...5).overlap?(5..10).should == false
|
85
126
|
end
|
86
127
|
end
|
Binary file
|
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.16
|
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-
|
14
|
+
date: 2011-09-02 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.
|
@@ -23,7 +23,7 @@ extensions: []
|
|
23
23
|
extra_rdoc_files: []
|
24
24
|
files:
|
25
25
|
- LICENSE
|
26
|
-
- README
|
26
|
+
- README.md
|
27
27
|
- lib/utilities.rb
|
28
28
|
- lib/utilities/array.rb
|
29
29
|
- lib/utilities/datetime.rb
|
@@ -38,7 +38,7 @@ files:
|
|
38
38
|
- lib/utilities/utilities.rb
|
39
39
|
- lib/version.rb
|
40
40
|
- test/test_utilities.rb
|
41
|
-
- utilities-0.0.
|
41
|
+
- utilities-0.0.15.gem
|
42
42
|
- utilities.gemspec
|
43
43
|
homepage: http://github.com/christianblais/utilities
|
44
44
|
licenses: []
|
data/README
DELETED
File without changes
|
data/utilities-0.0.14.gem
DELETED
Binary file
|