ruby-nuggets 0.6.9 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.rspec +1 -0
  2. data/README +1 -1
  3. data/Rakefile +4 -4
  4. data/lib/nuggets/array/correlation.rb +5 -0
  5. data/lib/nuggets/array/correlation_mixin.rb +62 -0
  6. data/lib/nuggets/array/histogram.rb +5 -0
  7. data/lib/nuggets/array/histogram_mixin.rb +155 -0
  8. data/lib/nuggets/array/mean.rb +5 -0
  9. data/lib/nuggets/array/mean_mixin.rb +139 -0
  10. data/lib/nuggets/array/median.rb +5 -0
  11. data/lib/nuggets/array/median_mixin.rb +74 -0
  12. data/lib/nuggets/array/mode.rb +5 -0
  13. data/lib/nuggets/array/mode_mixin.rb +70 -0
  14. data/lib/nuggets/array/monotone.rb +5 -5
  15. data/lib/nuggets/array/only.rb +3 -2
  16. data/lib/nuggets/array/regression.rb +5 -0
  17. data/lib/nuggets/array/regression_mixin.rb +63 -0
  18. data/lib/nuggets/array/runiq_mixin.rb +1 -1
  19. data/lib/nuggets/array/standard_deviation_mixin.rb +5 -8
  20. data/lib/nuggets/array/variance_mixin.rb +30 -11
  21. data/lib/nuggets/enumerable/all_any_extended.rb +6 -6
  22. data/lib/nuggets/enumerable/minmax.rb +2 -2
  23. data/lib/nuggets/env/set_mixin.rb +4 -4
  24. data/lib/nuggets/env/user_home_mixin.rb +1 -1
  25. data/lib/nuggets/file/replace_mixin.rb +3 -3
  26. data/lib/nuggets/file/sub_mixin.rb +2 -2
  27. data/lib/nuggets/file/which_mixin.rb +15 -14
  28. data/lib/nuggets/hash/nest_mixin.rb +3 -3
  29. data/lib/nuggets/hash/only.rb +6 -4
  30. data/lib/nuggets/hash/unroll_mixin.rb +1 -1
  31. data/lib/nuggets/io/modes.rb +12 -12
  32. data/lib/nuggets/numeric/duration.rb +3 -3
  33. data/lib/nuggets/object/blank_mixin.rb +6 -6
  34. data/lib/nuggets/object/boolean_mixin.rb +3 -3
  35. data/lib/nuggets/object/singleton_class_mixin.rb +3 -3
  36. data/lib/nuggets/range/quantile_mixin.rb +1 -1
  37. data/lib/nuggets/statistics.rb +11 -0
  38. data/lib/nuggets/statistics_mixins.rb +11 -0
  39. data/lib/nuggets/string/case.rb +4 -4
  40. data/lib/nuggets/string/msub.rb +1 -1
  41. data/lib/nuggets/string/nsub.rb +2 -2
  42. data/lib/nuggets/string/sub_with_md.rb +2 -2
  43. data/lib/nuggets/tempfile/open.rb +1 -1
  44. data/lib/nuggets/uri/content_type_mixin.rb +1 -1
  45. data/lib/nuggets/uri/exist_mixin.rb +2 -2
  46. data/lib/nuggets/util/content_type.rb +1 -1
  47. data/lib/nuggets/util/i18n.rb +63 -63
  48. data/lib/nuggets/util/ruby.rb +7 -7
  49. data/lib/nuggets/version.rb +2 -2
  50. data/spec/nuggets/array/mean_spec.rb +127 -0
  51. data/spec/nuggets/array/median_spec.rb +79 -0
  52. data/spec/nuggets/array/mode_spec.rb +59 -0
  53. data/spec/nuggets/string/evaluate_spec.rb +2 -2
  54. metadata +29 -15
@@ -109,8 +109,8 @@ module Util
109
109
 
110
110
  # Returns whether the Ruby interpreter supports process forking.
111
111
  def ruby_supports_fork?
112
- # MRI >= 1.9.2's respond_to? returns false for methods
113
- # that are not implemented.
112
+ # MRI >= 1.9.2's respond_to? returns false
113
+ # for methods that are not implemented.
114
114
  Process.respond_to?(:fork) &&
115
115
  RUBY_ENGINE != 'jruby' &&
116
116
  RUBY_ENGINE != 'macruby' &&
@@ -124,7 +124,7 @@ module Util
124
124
 
125
125
  # If the current Ruby interpreter is managed by RVM, returns the
126
126
  # directory in which RVM places its working files. Otherwise returns
127
- # nil.
127
+ # +nil+.
128
128
  def rvm_path
129
129
  return @rvm_path if defined?(@rvm_path)
130
130
 
@@ -147,7 +147,7 @@ module Util
147
147
  # currently active gemset, e.g. something like this:
148
148
  # "ruby-1.9.2-p0@mygemset"
149
149
  #
150
- # Returns nil otherwise.
150
+ # Returns +nil+ otherwise.
151
151
  def rvm_ruby_string
152
152
  return @rvm_ruby_string if defined?(@rvm_ruby_string)
153
153
 
@@ -194,7 +194,7 @@ module Util
194
194
  # NOTE: The return value may not be the actual correct invocation for the tool.
195
195
  # Use command_for_ruby_tool for that.
196
196
  #
197
- # Returns nil when nothing's found.
197
+ # Returns +nil+ when nothing's found.
198
198
  def locate_ruby_tool(name)
199
199
  extensions = ['', CONFIG['EXEEXT']].compact.uniq
200
200
 
@@ -213,8 +213,8 @@ module Util
213
213
  end
214
214
 
215
215
  # Returns the correct command string for invoking the +name+ executable
216
- # that belongs to the current Ruby interpreter. Returns nil if the command
217
- # is not found.
216
+ # that belongs to the current Ruby interpreter. Returns +nil+ if the
217
+ # command is not found.
218
218
  #
219
219
  # If the command executable is a Ruby program, then we need to run it
220
220
  # in the correct Ruby interpreter just in case the command doesn't
@@ -3,8 +3,8 @@ module Nuggets
3
3
  module Version
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 6
7
- TINY = 9
6
+ MINOR = 7
7
+ TINY = 0
8
8
 
9
9
  class << self
10
10
 
@@ -0,0 +1,127 @@
1
+ require 'nuggets/array/mean'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::MeanMixin) }
6
+
7
+ describe 'equalities' do
8
+
9
+ %w[arithmetic geometric harmonic].each { |type|
10
+ method = "#{type}_mean"
11
+
12
+ example do
13
+ val = rand
14
+ ary = [val] * 10
15
+
16
+ ary.send(method).should equal_float(val)
17
+ end
18
+
19
+ example do
20
+ ary, const = Array.new(10) { rand }, rand
21
+
22
+ res1 = ary.map { |i| const * i }.send(method)
23
+ res2 = ary.send(method)
24
+
25
+ res1.should equal_float(const * res2)
26
+ end
27
+ }
28
+
29
+ end
30
+
31
+ describe 'inequalities' do
32
+
33
+ before :each do
34
+ @ary = Array.new(10) { rand }
35
+ end
36
+
37
+ 3.times {
38
+
39
+ { 0 => 1, 1 => 2, 3 => 23, 0.1 => 0.2, -1 => 0, -3 => -2 }.each { |exponent1, exponent2|
40
+ example do
41
+ @ary.generalized_mean(exponent1).should <= @ary.generalized_mean(exponent2)
42
+ end
43
+ }
44
+
45
+ example do
46
+ res = %w[quadratic arithmetic geometric harmonic].map { |type| @ary.send("#{type}_mean") }
47
+ res[0].should >= res[1]; res[1].should >= res[2]; res[2].should >= res[3]
48
+ end
49
+
50
+ }
51
+
52
+ end
53
+
54
+ describe 'arithmetic mean' do
55
+
56
+ example do
57
+ [].arithmetic_mean.should be_nil
58
+ end
59
+
60
+ example do
61
+ [1].arithmetic_mean.should == 1.0
62
+ end
63
+
64
+ example do
65
+ [1, 1, 1].arithmetic_mean.should == 1.0
66
+ end
67
+
68
+ example do
69
+ [1, 2, 3].arithmetic_mean.should == 2.0
70
+ end
71
+
72
+ example do
73
+ [3, 2, 1].arithmetic_mean.should == 2.0
74
+ end
75
+
76
+ example do
77
+ [-3, -2, -1].arithmetic_mean.should == -[3, 2, 1].arithmetic_mean
78
+ end
79
+
80
+ context do
81
+
82
+ before :each do
83
+ @ary = [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24]
84
+ @res = @ary.arithmetic_mean
85
+ end
86
+
87
+ example do
88
+ @res.should equal_float(2.46153846153846)
89
+ end
90
+
91
+ %w[mean average avg].each { |method|
92
+ example { @ary.send(method).should == @res }
93
+ }
94
+
95
+ end
96
+
97
+ describe '(weighted)' do
98
+
99
+ example do
100
+ [1, 2, 3].arithmetic_mean { |i| 0 }.should be_nil
101
+ end
102
+
103
+ example do
104
+ [1, 2, 3].arithmetic_mean { |i| 1 / i.to_f }.should equal_float(1.63636363636364)
105
+ end
106
+
107
+ example do
108
+ w = rand
109
+ [1, 2, 3].arithmetic_mean { |i| w }.should == 2.0
110
+ end
111
+
112
+ example do
113
+ [1, 2, 3].arithmetic_mean { |i| i ** 2 }.should equal_float(2.57142857142857)
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ # TODO: other methods
121
+ # TODO: more examples: http://people.revoledu.com/kardi/tutorial/BasicMath/Average/mean.html
122
+
123
+ def equal_float(value, precision = 1.0e-14)
124
+ be_within(precision).of(value)
125
+ end
126
+
127
+ end
@@ -0,0 +1,79 @@
1
+ require 'nuggets/array/median'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::MedianMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::MedianMixin) }
6
+
7
+ example do
8
+ [].median.should be_nil
9
+ end
10
+
11
+ example do
12
+ [1].median.should == 1
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].median.should == 1
17
+ end
18
+
19
+ example do
20
+ [1, 2, 3].median.should == 2
21
+ end
22
+
23
+ example do
24
+ [3, 2, 1].median.should == 2
25
+ end
26
+
27
+ example do
28
+ [-3, -2, -1].median.should == -[3, 2, 1].median
29
+ end
30
+
31
+ example do
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].median.should == 1
33
+ end
34
+
35
+ example do
36
+ [1, 2, 3, 4].median.should == 2.5
37
+ end
38
+
39
+ example do
40
+ %w[one two three].median.should == 'three'
41
+ end
42
+
43
+ example do
44
+ %w[three two one].median.should == 'three'
45
+ end
46
+
47
+ [true, 1, :left].each { |prefer|
48
+ example { [1, 2, 3].median(prefer).should == 2 }
49
+ example { [1, 2, 3, 4].median(prefer).should == 2 }
50
+
51
+ example { %w[one two three].median(prefer).should == 'three' }
52
+ example { %w[one two three four].median(prefer).should == 'one' }
53
+ }
54
+
55
+ [false, 2, :right].each { |prefer|
56
+ example { [1, 2, 3].median(prefer).should == 2 }
57
+ example { [1, 2, 3, 4].median(prefer).should == 3 }
58
+
59
+ example { %w[one two three].median(prefer).should == 'three' }
60
+ example { %w[one two three four].median(prefer).should == 'three' }
61
+ }
62
+
63
+ example do
64
+ [1, 2, 3, 4].median { |a, b| a * b }.should == 6
65
+ end
66
+
67
+ example do
68
+ %w[one two three four].median { |a, b| b * a.length }.should == 'threethreethree'
69
+ end
70
+
71
+ example do
72
+ lambda { %w[one two three four].median }.should raise_error(NoMethodError, %q{undefined method `/' for "onethree":String})
73
+ end
74
+
75
+ example do
76
+ lambda { [1, 2, 'three'].median }.should raise_error(ArgumentError, 'comparison of Fixnum with String failed')
77
+ end
78
+
79
+ end
@@ -0,0 +1,59 @@
1
+ require 'nuggets/array/mode'
2
+
3
+ describe Array, 'when extended by', Nuggets::Array::ModeMixin do
4
+
5
+ it { Array.ancestors.should include(Nuggets::Array::ModeMixin) }
6
+
7
+ example do
8
+ [].mode.should be_nil
9
+ end
10
+
11
+ example do
12
+ [1].mode.should == 1
13
+ end
14
+
15
+ example do
16
+ [1, 1, 1].mode.should == 1
17
+ end
18
+
19
+ example do
20
+ [1, 2, 2, 3].mode.should == 2
21
+ end
22
+
23
+ example do
24
+ [3, 2, 2, 1].mode.should == 2
25
+ end
26
+
27
+ example do
28
+ [-3, -2, -2, -1].mode.should == -[3, 2, 2, 1].mode
29
+ end
30
+
31
+ example do
32
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].mode.should == 1
33
+ end
34
+
35
+ example do
36
+ [1, 2, 2, 3, 3, 4].modes.should == [2, 3]
37
+ end
38
+
39
+ example do
40
+ [2, 3].should include([1, 2, 2, 3, 3, 4].mode)
41
+ end
42
+
43
+ example do
44
+ %w[one two three three].mode.should == 'three'
45
+ end
46
+
47
+ example do
48
+ %w[three two one three].mode.should == 'three'
49
+ end
50
+
51
+ example do
52
+ [1, 2, 2, 3, 4].mode { |x| x * 2 }.should == 4
53
+ end
54
+
55
+ example do
56
+ [1, 2, 3, 4].modes { |x| x / 2 }.should == [1]
57
+ end
58
+
59
+ end
@@ -11,9 +11,9 @@ describe String, 'when extended by', Nuggets::String::EvaluateMixin do
11
11
  example { str.evaluate(binding).should == res }
12
12
  }
13
13
 
14
- example {
14
+ example do
15
15
  lambda { str.evaluate(binding) }.should raise_error(NameError, /`a'/)
16
- }
16
+ end
17
17
  }
18
18
 
19
19
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 9
10
- version: 0.6.9
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-11 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-04-05 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: Some extensions to the Ruby programming language.
@@ -26,9 +25,9 @@ executables: []
26
25
  extensions: []
27
26
 
28
27
  extra_rdoc_files:
28
+ - README
29
29
  - COPYING
30
30
  - ChangeLog
31
- - README
32
31
  files:
33
32
  - lib/nuggets.rb
34
33
  - lib/nuggets/integer/map_mixin.rb
@@ -45,24 +44,37 @@ files:
45
44
  - lib/nuggets/util/ansicolor2css.rb
46
45
  - lib/nuggets/util/added_methods.rb
47
46
  - lib/nuggets/array/shuffle.rb
47
+ - lib/nuggets/array/mean.rb
48
48
  - lib/nuggets/array/variance_mixin.rb
49
+ - lib/nuggets/array/mode_mixin.rb
50
+ - lib/nuggets/array/median.rb
49
51
  - lib/nuggets/array/format.rb
52
+ - lib/nuggets/array/mode.rb
50
53
  - lib/nuggets/array/to_hash.rb
51
54
  - lib/nuggets/array/rand.rb
55
+ - lib/nuggets/array/median_mixin.rb
52
56
  - lib/nuggets/array/runiq.rb
57
+ - lib/nuggets/array/mean_mixin.rb
58
+ - lib/nuggets/array/regression_mixin.rb
53
59
  - lib/nuggets/array/monotone.rb
54
60
  - lib/nuggets/array/combination.rb
55
61
  - lib/nuggets/array/flatten_once.rb
56
62
  - lib/nuggets/array/standard_deviation_mixin.rb
63
+ - lib/nuggets/array/histogram_mixin.rb
57
64
  - lib/nuggets/array/variance.rb
58
65
  - lib/nuggets/array/limit.rb
66
+ - lib/nuggets/array/correlation.rb
59
67
  - lib/nuggets/array/standard_deviation.rb
68
+ - lib/nuggets/array/histogram.rb
69
+ - lib/nuggets/array/correlation_mixin.rb
60
70
  - lib/nuggets/array/limit_mixin.rb
61
71
  - lib/nuggets/array/in_order.rb
62
72
  - lib/nuggets/array/runiq_mixin.rb
73
+ - lib/nuggets/array/regression.rb
63
74
  - lib/nuggets/array/only.rb
64
75
  - lib/nuggets/proc/bind_mixin.rb
65
76
  - lib/nuggets/proc/bind.rb
77
+ - lib/nuggets/statistics.rb
66
78
  - lib/nuggets/hash/nest_mixin.rb
67
79
  - lib/nuggets/hash/insert.rb
68
80
  - lib/nuggets/hash/unroll.rb
@@ -119,6 +131,7 @@ files:
119
131
  - lib/nuggets/range/quantile.rb
120
132
  - lib/nuggets/all.rb
121
133
  - lib/nuggets/version.rb
134
+ - lib/nuggets/statistics_mixins.rb
122
135
  - lib/nuggets/object/virtual_class.rb
123
136
  - lib/nuggets/object/uniclass.rb
124
137
  - lib/nuggets/object/blank.rb
@@ -138,11 +151,13 @@ files:
138
151
  - ChangeLog
139
152
  - Rakefile
140
153
  - COPYING
141
- - .rspec
142
154
  - spec/nuggets/integer/map_spec.rb
143
155
  - spec/nuggets/integer/length_spec.rb
156
+ - spec/nuggets/array/mode_spec.rb
144
157
  - spec/nuggets/array/variance_spec.rb
145
158
  - spec/nuggets/array/runiq_spec.rb
159
+ - spec/nuggets/array/median_spec.rb
160
+ - spec/nuggets/array/mean_spec.rb
146
161
  - spec/nuggets/array/limit_spec.rb
147
162
  - spec/nuggets/array/standard_deviation_spec.rb
148
163
  - spec/nuggets/proc/bind_spec.rb
@@ -167,21 +182,20 @@ files:
167
182
  - spec/nuggets/object/silence_spec.rb
168
183
  - spec/nuggets/object/blank_spec.rb
169
184
  - spec/spec_helper.rb
170
- has_rdoc: true
185
+ - .rspec
171
186
  homepage: http://prometheus.rubyforge.org/ruby-nuggets
172
187
  licenses: []
173
188
 
174
189
  post_install_message:
175
190
  rdoc_options:
176
- - --charset
177
- - UTF-8
191
+ - --line-numbers
178
192
  - --main
179
193
  - README
194
+ - --charset
195
+ - UTF-8
180
196
  - --all
181
- - --line-numbers
182
197
  - --title
183
- - ruby-nuggets Application documentation (v0.6.9)
184
- - --inline-source
198
+ - ruby-nuggets Application documentation (v0.7.0)
185
199
  require_paths:
186
200
  - lib
187
201
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -205,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
219
  requirements: []
206
220
 
207
221
  rubyforge_project: prometheus
208
- rubygems_version: 1.5.2
222
+ rubygems_version: 1.7.1
209
223
  signing_key:
210
224
  specification_version: 3
211
225
  summary: Some extensions to the Ruby programming language.