davidrichards-just_enumerable_stats 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/just_enumerable_stats.rb +17 -1
- data/spec/just_enumerable_stats_spec.rb +23 -6
- data/spec/spec_helper.rb +2 -0
- data/spec/unobtrusive_just_enumerable_stats_spec.rb +8 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'mathn'
|
2
3
|
|
3
4
|
# Need the FixedRange
|
4
5
|
$:.unshift File.dirname(__FILE__)
|
@@ -765,7 +766,22 @@ module Enumerable
|
|
765
766
|
self._jes_scale!{|e| (e * scalar) + shift}
|
766
767
|
end
|
767
768
|
safe_alias :_jes_scale_between!
|
769
|
+
|
770
|
+
# Returns a hash or dictionary (if installed) of the frequency of each category.
|
771
|
+
def _jes_frequency
|
772
|
+
dict = defined?(Dictionary) ? Dictionary.new : Hash.new
|
773
|
+
self._jes_category_values.each do |k, v|
|
774
|
+
dict[k] = v.size / self.size
|
775
|
+
end
|
776
|
+
dict
|
777
|
+
end
|
778
|
+
safe_alias :_jes_frequency
|
779
|
+
|
780
|
+
def _jes_frequency_for(key)
|
781
|
+
self._jes_frequency[key]
|
782
|
+
end
|
783
|
+
safe_alias :_jes_frequency_for
|
768
784
|
|
769
785
|
end
|
770
786
|
|
771
|
-
@a = [1,2,3]
|
787
|
+
# @a = [1,2,3]
|
@@ -162,19 +162,19 @@ describe "JustEnumerableStats" do
|
|
162
162
|
end
|
163
163
|
|
164
164
|
it "should be able to calculate the standard deviation" do
|
165
|
-
@a.standard_deviation.should eql(1
|
166
|
-
@a.std.should eql(1
|
165
|
+
@a.standard_deviation.should eql(1)
|
166
|
+
@a.std.should eql(1)
|
167
167
|
end
|
168
168
|
|
169
169
|
it "should be able to calculate the standard deviation with a block" do
|
170
|
-
@a.standard_deviation(&@doubler).should eql(2
|
171
|
-
@a.std(&@doubler).should eql(2
|
170
|
+
@a.standard_deviation(&@doubler).should eql(2)
|
171
|
+
@a.std(&@doubler).should eql(2)
|
172
172
|
end
|
173
173
|
|
174
174
|
it "should be able to calculate the standard deviation with a default block" do
|
175
175
|
@a.default_block = @doubler
|
176
|
-
@a.standard_deviation.should eql(2
|
177
|
-
@a.std.should eql(2
|
176
|
+
@a.standard_deviation.should eql(2)
|
177
|
+
@a.std.should eql(2)
|
178
178
|
end
|
179
179
|
|
180
180
|
it "should be able to calculate the median value" do
|
@@ -316,6 +316,10 @@ describe "JustEnumerableStats" do
|
|
316
316
|
@b.rank.should eql([1,2,3])
|
317
317
|
end
|
318
318
|
|
319
|
+
it "should have ordinalize work like ranke" do
|
320
|
+
@b.ordinalize.should eql([3,2,1])
|
321
|
+
end
|
322
|
+
|
319
323
|
it "should be able to get the order of values, handling duplicates" do
|
320
324
|
[10,5,5,1].order.should eql([4,2,3,1])
|
321
325
|
end
|
@@ -601,4 +605,17 @@ describe "JustEnumerableStats" do
|
|
601
605
|
@a.should eql(b)
|
602
606
|
end
|
603
607
|
end
|
608
|
+
|
609
|
+
context "frequency" do
|
610
|
+
it "should be able to generate a frequency" do
|
611
|
+
@a.frequency[1].should eql(1/3)
|
612
|
+
@a.frequency[2].should eql(1/3)
|
613
|
+
@a.frequency[3].should eql(1/3)
|
614
|
+
end
|
615
|
+
|
616
|
+
it "should be able to generate a frequency for a specific value" do
|
617
|
+
a = [3,3,3,2]
|
618
|
+
a.frequency_for(3).should eql(3/4)
|
619
|
+
end
|
620
|
+
end
|
604
621
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -72,6 +72,8 @@ class BusyClass
|
|
72
72
|
def normalize!; raise ArgumentError, "Should not be called"; end
|
73
73
|
def scale_between; raise ArgumentError, "Should not be called"; end
|
74
74
|
def scale_between!; raise ArgumentError, "Should not be called"; end
|
75
|
+
def frequency; raise ArgumentError, "Should not be called"; end
|
76
|
+
def frequency_for(val); raise ArgumentError, "Should not be called"; end
|
75
77
|
end
|
76
78
|
|
77
79
|
|
@@ -227,6 +227,14 @@ describe "JustEnumerableStats" do
|
|
227
227
|
it "should not use the native scale_between!" do
|
228
228
|
lambda{@a._jes_scale_between!(6,8)}.should_not raise_error
|
229
229
|
end
|
230
|
+
|
231
|
+
it "should not use the native frequency" do
|
232
|
+
lambda{@a._jes_frequency}.should_not raise_error
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should not use the native frequency_for" do
|
236
|
+
lambda{@a._jes_frequency_for(2)}.should_not raise_error
|
237
|
+
end
|
230
238
|
|
231
239
|
end
|
232
240
|
|