ruby-nuggets 0.8.6 → 0.8.7
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/README +1 -1
- data/lib/nuggets/array/mean_mixin.rb +46 -1
- data/lib/nuggets/util/cli.rb +18 -7
- data/lib/nuggets/util/i18n.rb +24 -2
- data/lib/nuggets/version.rb +1 -1
- data/spec/nuggets/array/mean_spec.rb +49 -0
- metadata +7 -7
data/README
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
5
|
# language. #
|
6
6
|
# #
|
7
|
-
# Copyright (C) 2007-
|
7
|
+
# Copyright (C) 2007-2012 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
10
|
# Jens Wille <jens.wille@uni-koeln.de> #
|
@@ -134,6 +134,51 @@ module Nuggets
|
|
134
134
|
|
135
135
|
alias_method :geomean, :geometric_mean
|
136
136
|
|
137
|
+
# call-seq:
|
138
|
+
# array.report_mean => anArray
|
139
|
+
# array.report_mean(method[, precision]) => anArray
|
140
|
+
#
|
141
|
+
# Expects _array_ to be an array of arrays ("rows") of numeric values;
|
142
|
+
# the first "row" may consist of strings (labels) instead. Returns an
|
143
|
+
# array of strings with the mean (according to +method+, default #mean)
|
144
|
+
# of each "column", prepended with the label, if present, and appended
|
145
|
+
# with the standard deviation, if available; all values are subject to
|
146
|
+
# +precision+.
|
147
|
+
#
|
148
|
+
# Examples:
|
149
|
+
#
|
150
|
+
# [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean
|
151
|
+
# #=> ["9.4567 +/- 0.0450", "34.6800 +/- 0.0572"]
|
152
|
+
#
|
153
|
+
# [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(:harmonic)
|
154
|
+
# #=> ["9.4565 +/- 0.0450", "34.6799 +/- 0.0572"]
|
155
|
+
#
|
156
|
+
# [["a", "b"], [9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean(nil, 2)
|
157
|
+
# #=> ["a 9.46 +/- 0.04", "b 34.68 +/- 0.06"]
|
158
|
+
#
|
159
|
+
# CSV.read('csv', headers: true, converters: :numeric).to_a.report_mean
|
160
|
+
# #=> ["a 9.4567 +/- 0.0450", "b 34.6800 +/- 0.0572"]
|
161
|
+
def report_mean(method = nil, precision = 4)
|
162
|
+
met, sep = [method ||= :mean, 'mean'], ['', '_']
|
163
|
+
lab, std = first.first.is_a?(::String), respond_to?(:std)
|
164
|
+
|
165
|
+
fmt = ["%-#{precision}s", "%.#{precision}f", "+/- %.#{precision}f"]
|
166
|
+
|
167
|
+
until respond_to?(method) || sep.empty?
|
168
|
+
method = met.join(sep.shift)
|
169
|
+
end
|
170
|
+
|
171
|
+
transpose.map! { |x|
|
172
|
+
i, a = [], []
|
173
|
+
|
174
|
+
i << 0 and a << x.shift if lab
|
175
|
+
i << 1 and a << x.send(method)
|
176
|
+
i << 2 and a << x.std if std
|
177
|
+
|
178
|
+
fmt.values_at(*i).join(' ') % a
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
137
182
|
end
|
138
183
|
end
|
139
184
|
end
|
data/lib/nuggets/util/cli.rb
CHANGED
@@ -120,8 +120,12 @@ module Util
|
|
120
120
|
::HighLine.new(stdin, stdout).ask(question, &block)
|
121
121
|
end
|
122
122
|
|
123
|
-
def
|
124
|
-
|
123
|
+
def puts(*msg)
|
124
|
+
stdout.puts(*msg)
|
125
|
+
end
|
126
|
+
|
127
|
+
def warn(*msg)
|
128
|
+
stderr.puts(*msg)
|
125
129
|
end
|
126
130
|
|
127
131
|
def quit(msg = nil, include_usage = msg != false)
|
@@ -138,6 +142,11 @@ module Util
|
|
138
142
|
exit(status)
|
139
143
|
end
|
140
144
|
|
145
|
+
def shut(msg = nil, status = 0)
|
146
|
+
puts(msg) if msg
|
147
|
+
exit(status)
|
148
|
+
end
|
149
|
+
|
141
150
|
def exit(status = 0)
|
142
151
|
::Kernel.exit(status)
|
143
152
|
end
|
@@ -157,11 +166,13 @@ module Util
|
|
157
166
|
end
|
158
167
|
end
|
159
168
|
|
160
|
-
def load_config(file = options[:config] || defaults[:config])
|
169
|
+
def load_config(file = options[:config] || default = defaults[:config])
|
170
|
+
return unless file
|
171
|
+
|
161
172
|
if ::File.readable?(file)
|
162
173
|
@config = ::YAML.load_file(file)
|
163
|
-
|
164
|
-
quit "No such file: #{file}"
|
174
|
+
else
|
175
|
+
quit "No such file: #{file}" unless default
|
165
176
|
end
|
166
177
|
end
|
167
178
|
|
@@ -205,11 +216,11 @@ module Util
|
|
205
216
|
|
206
217
|
def generic_opts(opts)
|
207
218
|
opts.on('-h', '--help', 'Print this help message and exit') {
|
208
|
-
|
219
|
+
shut opts
|
209
220
|
}
|
210
221
|
|
211
222
|
opts.on('--version', 'Print program version and exit') {
|
212
|
-
|
223
|
+
shut "#{progname} v#{version}"
|
213
224
|
}
|
214
225
|
end
|
215
226
|
|
data/lib/nuggets/util/i18n.rb
CHANGED
@@ -96,6 +96,17 @@ module Util
|
|
96
96
|
'ÿ' => 'y' # LATIN SMALL LETTER Y WITH DIAERESIS
|
97
97
|
}
|
98
98
|
|
99
|
+
def self.args_for_map_diacritics
|
100
|
+
@gsub_for_map_diacritics ||= begin
|
101
|
+
map = ::Hash.new { |h, k| h[k] = [] }
|
102
|
+
|
103
|
+
DIACRITICS.each { |a| a.each { |i| map[i].concat(a) } }
|
104
|
+
map.each { |k, v| v.uniq!; map[k] = "(#{::Regexp.union(*v).source})" }
|
105
|
+
|
106
|
+
[::Regexp.union(*map.keys.sort_by { |k| -k.length }), map.method(:[])]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
99
110
|
end
|
100
111
|
|
101
112
|
end
|
@@ -116,8 +127,10 @@ class String
|
|
116
127
|
#
|
117
128
|
# Destructive version of #replace_diacritics.
|
118
129
|
def replace_diacritics!
|
119
|
-
|
120
|
-
|
130
|
+
diacritics = ::Util::I18n::DIACRITICS
|
131
|
+
|
132
|
+
gsub!(/#{::Regexp.union(*diacritics.keys)}/) { |m|
|
133
|
+
s = diacritics[m]
|
121
134
|
|
122
135
|
# Try to adjust case:
|
123
136
|
# 'Äh' => 'AEh' => 'Aeh'
|
@@ -133,6 +146,15 @@ class String
|
|
133
146
|
}
|
134
147
|
end
|
135
148
|
|
149
|
+
def map_diacritics
|
150
|
+
(_dup = dup).map_diacritics! || _dup
|
151
|
+
end
|
152
|
+
|
153
|
+
def map_diacritics!
|
154
|
+
re, block = ::Util::I18n.args_for_map_diacritics
|
155
|
+
gsub!(re, &block)
|
156
|
+
end
|
157
|
+
|
136
158
|
end
|
137
159
|
|
138
160
|
if $0 == __FILE__
|
data/lib/nuggets/version.rb
CHANGED
@@ -120,4 +120,53 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
|
120
120
|
# TODO: other methods
|
121
121
|
# TODO: more examples: http://people.revoledu.com/kardi/tutorial/BasicMath/Average/mean.html
|
122
122
|
|
123
|
+
describe 'report mean' do
|
124
|
+
|
125
|
+
before :each do
|
126
|
+
@ary = [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]]
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'w/o stddev' do
|
130
|
+
|
131
|
+
before :each do
|
132
|
+
class << @ary; undef_method :std; end if @ary.respond_to?(:std)
|
133
|
+
end
|
134
|
+
|
135
|
+
example do
|
136
|
+
@ary.report_mean.should == ['9.4567', '34.6800']
|
137
|
+
end
|
138
|
+
|
139
|
+
example do
|
140
|
+
@ary.report_mean(:harmonic).should == ['9.4565', '34.6799']
|
141
|
+
end
|
142
|
+
|
143
|
+
example do
|
144
|
+
@ary.unshift(%w[a b]).report_mean(nil, 2).should == ['a 9.46', 'b 34.68']
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'w/ stddev' do
|
150
|
+
|
151
|
+
before :each do
|
152
|
+
require 'nuggets/array/standard_deviation_mixin'
|
153
|
+
@ary.extend(Nuggets::Array::StandardDeviationMixin)
|
154
|
+
end
|
155
|
+
|
156
|
+
example do
|
157
|
+
@ary.report_mean.should == ['9.4567 +/- 0.0450', '34.6800 +/- 0.0572']
|
158
|
+
end
|
159
|
+
|
160
|
+
example do
|
161
|
+
@ary.report_mean(:harmonic).should == ['9.4565 +/- 0.0450', '34.6799 +/- 0.0572']
|
162
|
+
end
|
163
|
+
|
164
|
+
example do
|
165
|
+
@ary.unshift(%w[a b]).report_mean(nil, 2).should == ['a 9.46 +/- 0.04', 'b 34.68 +/- 0.06']
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
123
172
|
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:
|
4
|
+
hash: 49
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 7
|
10
|
+
version: 0.8.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jens Wille
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-04-
|
18
|
+
date: 2012-04-27 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Some extensions to the Ruby programming language.
|
@@ -206,14 +206,14 @@ licenses: []
|
|
206
206
|
|
207
207
|
post_install_message:
|
208
208
|
rdoc_options:
|
209
|
+
- --line-numbers
|
209
210
|
- --main
|
210
211
|
- README
|
212
|
+
- --all
|
211
213
|
- --charset
|
212
214
|
- UTF-8
|
213
215
|
- --title
|
214
|
-
- ruby-nuggets Application documentation (v0.8.
|
215
|
-
- --all
|
216
|
-
- --line-numbers
|
216
|
+
- ruby-nuggets Application documentation (v0.8.7)
|
217
217
|
require_paths:
|
218
218
|
- lib
|
219
219
|
required_ruby_version: !ruby/object:Gem::Requirement
|