nuggets 0.9.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +663 -0
- data/ChangeLog +7 -0
- data/README +74 -0
- data/Rakefile +50 -0
- data/lib/nuggets.rb +73 -0
- data/lib/nuggets/all.rb +35 -0
- data/lib/nuggets/all_mixins.rb +33 -0
- data/lib/nuggets/ansicolor2css.rb +124 -0
- data/lib/nuggets/argv/option.rb +3 -0
- data/lib/nuggets/argv/option_mixin.rb +84 -0
- data/lib/nuggets/array/boost.rb +5 -0
- data/lib/nuggets/array/boost_mixin.rb +77 -0
- data/lib/nuggets/array/combination.rb +62 -0
- data/lib/nuggets/array/correlation.rb +5 -0
- data/lib/nuggets/array/correlation_mixin.rb +63 -0
- data/lib/nuggets/array/flatten_once.rb +56 -0
- data/lib/nuggets/array/flush.rb +5 -0
- data/lib/nuggets/array/flush_mixin.rb +46 -0
- data/lib/nuggets/array/format.rb +65 -0
- data/lib/nuggets/array/hashify.rb +5 -0
- data/lib/nuggets/array/hashify_mixin.rb +47 -0
- data/lib/nuggets/array/histogram.rb +5 -0
- data/lib/nuggets/array/histogram_mixin.rb +169 -0
- data/lib/nuggets/array/in_order.rb +50 -0
- data/lib/nuggets/array/limit.rb +5 -0
- data/lib/nuggets/array/limit_mixin.rb +56 -0
- data/lib/nuggets/array/mean.rb +5 -0
- data/lib/nuggets/array/mean_mixin.rb +202 -0
- data/lib/nuggets/array/median.rb +5 -0
- data/lib/nuggets/array/median_mixin.rb +73 -0
- data/lib/nuggets/array/mode.rb +5 -0
- data/lib/nuggets/array/mode_mixin.rb +69 -0
- data/lib/nuggets/array/monotone.rb +83 -0
- data/lib/nuggets/array/only.rb +41 -0
- data/lib/nuggets/array/rand.rb +44 -0
- data/lib/nuggets/array/regression.rb +5 -0
- data/lib/nuggets/array/regression_mixin.rb +149 -0
- data/lib/nuggets/array/runiq.rb +5 -0
- data/lib/nuggets/array/runiq_mixin.rb +52 -0
- data/lib/nuggets/array/shuffle.rb +132 -0
- data/lib/nuggets/array/standard_deviation.rb +5 -0
- data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
- data/lib/nuggets/array/to_hash.rb +64 -0
- data/lib/nuggets/array/variance.rb +5 -0
- data/lib/nuggets/array/variance_mixin.rb +81 -0
- data/lib/nuggets/content_type.rb +97 -0
- data/lib/nuggets/dotted_decimal.rb +59 -0
- data/lib/nuggets/enumerable/agrep.rb +79 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
- data/lib/nuggets/enumerable/minmax.rb +102 -0
- data/lib/nuggets/env/set.rb +3 -0
- data/lib/nuggets/env/set_mixin.rb +67 -0
- data/lib/nuggets/env/user_encoding.rb +3 -0
- data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
- data/lib/nuggets/env/user_home.rb +3 -0
- data/lib/nuggets/env/user_home_mixin.rb +56 -0
- data/lib/nuggets/file/ext.rb +5 -0
- data/lib/nuggets/file/ext_mixin.rb +87 -0
- data/lib/nuggets/file/replace.rb +5 -0
- data/lib/nuggets/file/replace_mixin.rb +55 -0
- data/lib/nuggets/file/sub.rb +5 -0
- data/lib/nuggets/file/sub_mixin.rb +95 -0
- data/lib/nuggets/file/which.rb +5 -0
- data/lib/nuggets/file/which_mixin.rb +72 -0
- data/lib/nuggets/hash/at.rb +68 -0
- data/lib/nuggets/hash/deep_fetch.rb +5 -0
- data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
- data/lib/nuggets/hash/deep_merge.rb +5 -0
- data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
- data/lib/nuggets/hash/idmap.rb +5 -0
- data/lib/nuggets/hash/idmap_mixin.rb +41 -0
- data/lib/nuggets/hash/in_order.rb +43 -0
- data/lib/nuggets/hash/insert.rb +53 -0
- data/lib/nuggets/hash/nest.rb +5 -0
- data/lib/nuggets/hash/nest_mixin.rb +78 -0
- data/lib/nuggets/hash/only.rb +52 -0
- data/lib/nuggets/hash/seen.rb +5 -0
- data/lib/nuggets/hash/seen_mixin.rb +58 -0
- data/lib/nuggets/hash/unroll.rb +5 -0
- data/lib/nuggets/hash/unroll_mixin.rb +88 -0
- data/lib/nuggets/hash/zip.rb +5 -0
- data/lib/nuggets/hash/zip_mixin.rb +159 -0
- data/lib/nuggets/i18n.rb +155 -0
- data/lib/nuggets/integer/factorial.rb +56 -0
- data/lib/nuggets/integer/length.rb +5 -0
- data/lib/nuggets/integer/length_mixin.rb +49 -0
- data/lib/nuggets/integer/map.rb +5 -0
- data/lib/nuggets/integer/map_mixin.rb +42 -0
- data/lib/nuggets/integer/to_binary_s.rb +38 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/interact.rb +5 -0
- data/lib/nuggets/io/interact_mixin.rb +159 -0
- data/lib/nuggets/io/modes.rb +121 -0
- data/lib/nuggets/io/null.rb +5 -0
- data/lib/nuggets/io/null_mixin.rb +40 -0
- data/lib/nuggets/io/redirect.rb +5 -0
- data/lib/nuggets/io/redirect_mixin.rb +50 -0
- data/lib/nuggets/lazy_attr.rb +44 -0
- data/lib/nuggets/log_parser.rb +70 -0
- data/lib/nuggets/log_parser/apache.rb +101 -0
- data/lib/nuggets/log_parser/rails.rb +219 -0
- data/lib/nuggets/net/success.rb +59 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +100 -0
- data/lib/nuggets/numeric/limit.rb +62 -0
- data/lib/nuggets/numeric/signum.rb +52 -0
- data/lib/nuggets/numeric/to_multiple.rb +61 -0
- data/lib/nuggets/object/blank.rb +20 -0
- data/lib/nuggets/object/blank_mixin.rb +99 -0
- data/lib/nuggets/object/boolean.rb +5 -0
- data/lib/nuggets/object/boolean_mixin.rb +61 -0
- data/lib/nuggets/object/eigenclass.rb +2 -0
- data/lib/nuggets/object/ghost_class.rb +2 -0
- data/lib/nuggets/object/metaclass.rb +2 -0
- data/lib/nuggets/object/msend.rb +5 -0
- data/lib/nuggets/object/msend_mixin.rb +43 -0
- data/lib/nuggets/object/silence.rb +5 -0
- data/lib/nuggets/object/silence_mixin.rb +44 -0
- data/lib/nuggets/object/singleton_class.rb +5 -0
- data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/pluggable.rb +91 -0
- data/lib/nuggets/proc/bind.rb +5 -0
- data/lib/nuggets/proc/bind_mixin.rb +51 -0
- data/lib/nuggets/range/quantile.rb +5 -0
- data/lib/nuggets/range/quantile_mixin.rb +42 -0
- data/lib/nuggets/ruby.rb +235 -0
- data/lib/nuggets/statistics.rb +12 -0
- data/lib/nuggets/statistics_mixins.rb +12 -0
- data/lib/nuggets/string/camelscore.rb +5 -0
- data/lib/nuggets/string/camelscore_mixin.rb +116 -0
- data/lib/nuggets/string/capitalize_first.rb +46 -0
- data/lib/nuggets/string/case.rb +81 -0
- data/lib/nuggets/string/evaluate.rb +5 -0
- data/lib/nuggets/string/evaluate_mixin.rb +47 -0
- data/lib/nuggets/string/msub.rb +84 -0
- data/lib/nuggets/string/nsub.rb +65 -0
- data/lib/nuggets/string/sub_with_md.rb +111 -0
- data/lib/nuggets/string/wc.rb +5 -0
- data/lib/nuggets/string/wc_mixin.rb +95 -0
- data/lib/nuggets/string/word_wrap.rb +76 -0
- data/lib/nuggets/string/xor.rb +5 -0
- data/lib/nuggets/string/xor_mixin.rb +59 -0
- data/lib/nuggets/tempfile/open.rb +57 -0
- data/lib/nuggets/uri/content_type.rb +5 -0
- data/lib/nuggets/uri/content_type_mixin.rb +47 -0
- data/lib/nuggets/uri/exist.rb +5 -0
- data/lib/nuggets/uri/exist_mixin.rb +56 -0
- data/lib/nuggets/uri/redirect.rb +5 -0
- data/lib/nuggets/uri/redirect_mixin.rb +101 -0
- data/lib/nuggets/version.rb +27 -0
- data/spec/nuggets/array/boost_spec.rb +50 -0
- data/spec/nuggets/array/combination_spec.rb +25 -0
- data/spec/nuggets/array/correlation_spec.rb +81 -0
- data/spec/nuggets/array/flatten_once_spec.rb +16 -0
- data/spec/nuggets/array/flush_spec.rb +43 -0
- data/spec/nuggets/array/format_spec.rb +52 -0
- data/spec/nuggets/array/hashify_spec.rb +41 -0
- data/spec/nuggets/array/histogram_spec.rb +87 -0
- data/spec/nuggets/array/in_order_spec.rb +13 -0
- data/spec/nuggets/array/limit_spec.rb +62 -0
- data/spec/nuggets/array/mean_spec.rb +203 -0
- data/spec/nuggets/array/median_spec.rb +77 -0
- data/spec/nuggets/array/mode_spec.rb +57 -0
- data/spec/nuggets/array/monotone_spec.rb +30 -0
- data/spec/nuggets/array/only_spec.rb +26 -0
- data/spec/nuggets/array/regression_spec.rb +54 -0
- data/spec/nuggets/array/runiq_spec.rb +25 -0
- data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
- data/spec/nuggets/array/to_hash_spec.rb +28 -0
- data/spec/nuggets/array/variance_spec.rb +106 -0
- data/spec/nuggets/dotted_decimal_spec.rb +27 -0
- data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
- data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
- data/spec/nuggets/env/set_spec.rb +29 -0
- data/spec/nuggets/env/user_encoding_spec.rb +38 -0
- data/spec/nuggets/env/user_home_spec.rb +42 -0
- data/spec/nuggets/file/ext_spec.rb +38 -0
- data/spec/nuggets/file/replace_spec.rb +95 -0
- data/spec/nuggets/file/sub_spec.rb +149 -0
- data/spec/nuggets/file/which_spec.rb +22 -0
- data/spec/nuggets/hash/at_spec.rb +19 -0
- data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
- data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
- data/spec/nuggets/hash/in_order_spec.rb +12 -0
- data/spec/nuggets/hash/insert_spec.rb +13 -0
- data/spec/nuggets/hash/nest_spec.rb +102 -0
- data/spec/nuggets/hash/only_spec.rb +29 -0
- data/spec/nuggets/hash/seen_spec.rb +36 -0
- data/spec/nuggets/hash/unroll_spec.rb +68 -0
- data/spec/nuggets/i18n_spec.rb +13 -0
- data/spec/nuggets/integer/factorial_spec.rb +10 -0
- data/spec/nuggets/integer/length_spec.rb +18 -0
- data/spec/nuggets/integer/map_spec.rb +19 -0
- data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
- data/spec/nuggets/numeric/duration_spec.rb +25 -0
- data/spec/nuggets/numeric/limit_spec.rb +16 -0
- data/spec/nuggets/numeric/signum_spec.rb +16 -0
- data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
- data/spec/nuggets/object/blank_spec.rb +34 -0
- data/spec/nuggets/object/boolean_spec.rb +23 -0
- data/spec/nuggets/object/msend_spec.rb +25 -0
- data/spec/nuggets/object/silence_spec.rb +36 -0
- data/spec/nuggets/object/singleton_class_spec.rb +51 -0
- data/spec/nuggets/proc/bind_spec.rb +28 -0
- data/spec/nuggets/range/quantile_spec.rb +33 -0
- data/spec/nuggets/string/camelscore_spec.rb +114 -0
- data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
- data/spec/nuggets/string/case_spec.rb +31 -0
- data/spec/nuggets/string/evaluate_spec.rb +24 -0
- data/spec/nuggets/string/msub_spec.rb +20 -0
- data/spec/nuggets/string/nsub_spec.rb +13 -0
- data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
- data/spec/nuggets/string/wc_spec.rb +73 -0
- data/spec/nuggets/string/word_wrap_spec.rb +81 -0
- data/spec/nuggets/string/xor_spec.rb +57 -0
- data/spec/nuggets/uri/content_type_spec.rb +42 -0
- data/spec/nuggets/uri/exist_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- metadata +309 -17
@@ -0,0 +1,52 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
class Array
|
29
|
+
module RuniqMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# array.runiq => an_array
|
33
|
+
#
|
34
|
+
# Reverse #uniq.
|
35
|
+
def runiq
|
36
|
+
reverse.uniq.reverse
|
37
|
+
end
|
38
|
+
|
39
|
+
# call-seq:
|
40
|
+
# array.runiq! => an_array or +nil+
|
41
|
+
#
|
42
|
+
# Reverse #uniq!.
|
43
|
+
def runiq!
|
44
|
+
reverse!
|
45
|
+
res = uniq!
|
46
|
+
reverse!
|
47
|
+
res && self
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
class Array
|
28
|
+
|
29
|
+
# call-seq:
|
30
|
+
# array.shuffle => new_array
|
31
|
+
#
|
32
|
+
# Shuffles _array_ in random order. Select a different shuffling algorithm:
|
33
|
+
# <tt>Array.send(:alias_method, :shuffle, :shuffle_kfy)</tt>.
|
34
|
+
def shuffle
|
35
|
+
sort_by { ::Kernel.rand }
|
36
|
+
end
|
37
|
+
|
38
|
+
# call-seq:
|
39
|
+
# array.shuffle_knuth => new_array
|
40
|
+
#
|
41
|
+
# Non-destructive version of #shuffle_knuth!.
|
42
|
+
def shuffle_knuth
|
43
|
+
dup.shuffle_knuth!
|
44
|
+
end
|
45
|
+
|
46
|
+
# call-seq:
|
47
|
+
# array.shuffle_kfy => new_array
|
48
|
+
#
|
49
|
+
# Non-destructive version of #shuffle_kfy!.
|
50
|
+
def shuffle_kfy
|
51
|
+
dup.shuffle_kfy!
|
52
|
+
end
|
53
|
+
|
54
|
+
# call-seq:
|
55
|
+
# array.shuffle! => array
|
56
|
+
#
|
57
|
+
# Destructive version of #shuffle.
|
58
|
+
def shuffle!
|
59
|
+
replace shuffle
|
60
|
+
end
|
61
|
+
|
62
|
+
# call-seq:
|
63
|
+
# array.shuffle_knuth! => array
|
64
|
+
#
|
65
|
+
# Shuffles _array_ in random order using Knuth's algorithm.
|
66
|
+
def shuffle_knuth!
|
67
|
+
0.upto(length - 2) { |i|
|
68
|
+
n = i + rand(length - i)
|
69
|
+
self[i], self[n] = self[n], self[i]
|
70
|
+
}
|
71
|
+
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
# call-seq:
|
76
|
+
# array.shuffle_kfy! => array
|
77
|
+
#
|
78
|
+
# Shuffles _array_ in random order using the Knuth-Fisher-Yates algorithm.
|
79
|
+
def shuffle_kfy!
|
80
|
+
(length - 1).downto(0) { |i|
|
81
|
+
n = rand(i + 1)
|
82
|
+
self[n], self[i] = self[i], self[n]
|
83
|
+
}
|
84
|
+
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
if $0 == __FILE__
|
91
|
+
a = %w[1 2 3 4 5 6 7 8]
|
92
|
+
p a
|
93
|
+
|
94
|
+
p a.shuffle
|
95
|
+
p a.shuffle
|
96
|
+
|
97
|
+
p a.shuffle_knuth
|
98
|
+
p a.shuffle_kfy
|
99
|
+
|
100
|
+
a.shuffle!
|
101
|
+
p a
|
102
|
+
|
103
|
+
require 'nuggets/integer/factorial'
|
104
|
+
require 'nuggets/enumerable/minmax'
|
105
|
+
|
106
|
+
a = %w[a b c]
|
107
|
+
n = 100_000
|
108
|
+
m = a.length.f!
|
109
|
+
e = n / m.to_f
|
110
|
+
puts '%d / %d / %d / %.2f' % [a.length, n, m, e]
|
111
|
+
|
112
|
+
algorithms = %w[shuffle shuffle_knuth shuffle_kfy]
|
113
|
+
max = algorithms.max(:length)
|
114
|
+
|
115
|
+
algorithms.each { |algorithm|
|
116
|
+
score = ::Hash.new { |h, k| h[k] = 0 }
|
117
|
+
|
118
|
+
n.times {
|
119
|
+
score[a.send(algorithm)] += 1
|
120
|
+
}
|
121
|
+
|
122
|
+
x2 = 0
|
123
|
+
score.sort.each { |key, value|
|
124
|
+
x = value - e
|
125
|
+
y = x ** 2 / e
|
126
|
+
#puts '%s: %d (% .2f/%.2f)' % [key, value, x, y]
|
127
|
+
|
128
|
+
x2 += y
|
129
|
+
}
|
130
|
+
puts "%-#{max}s = %.2f (%.2f)" % [algorithm, x2, x2 / m]
|
131
|
+
}
|
132
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
require 'nuggets/array/variance_mixin'
|
28
|
+
|
29
|
+
module Nuggets
|
30
|
+
class Array
|
31
|
+
module StandardDeviationMixin
|
32
|
+
|
33
|
+
def self.included(base)
|
34
|
+
base.send :include, Nuggets::Array::VarianceMixin
|
35
|
+
end
|
36
|
+
|
37
|
+
# call-seq:
|
38
|
+
# array.standard_deviation => aFloat
|
39
|
+
#
|
40
|
+
# Calculates the {standard deviation}[http://en.wikipedia.org/wiki/Standard_deviation]
|
41
|
+
# of the values in _array_.
|
42
|
+
def standard_deviation(&block)
|
43
|
+
::Math.sqrt(variance(&block))
|
44
|
+
end
|
45
|
+
|
46
|
+
alias_method :std, :standard_deviation
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
require 'nuggets/array/flatten_once'
|
28
|
+
|
29
|
+
class Array
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# array.to_hash => aHash
|
33
|
+
# array.to_hash(value) => aHash
|
34
|
+
# array.to_hash { |element| ... } => aHash
|
35
|
+
#
|
36
|
+
# If neither +value+ nor block is given, converts _array_, taken as an
|
37
|
+
# array of key/value pairs, into a hash, preserving sub-arrays (Thus:
|
38
|
+
# <tt>hash.to_a.to_h == hash</tt>). Otherwise, maps each element of
|
39
|
+
# _array_ to +value+ or the result of the block.
|
40
|
+
#
|
41
|
+
# Examples:
|
42
|
+
# [[0, 0], [1, [2, 3]]].to_h #=> { 0 => 0, 1 => [2, 3] }
|
43
|
+
# %w[a b c d].to_h #=> { "a" => "b", "c" => "d" }
|
44
|
+
# %w[a b c d].to_h(1) #=> { "a" => 1, "b" => 1, "c" => 1, "d" => 1 }
|
45
|
+
# %w[a b].to_h { |e| e * 2 } #=> { "a" => "aa", "b" => "bb" }
|
46
|
+
def to_hash(value = default = true)
|
47
|
+
hash = {}
|
48
|
+
|
49
|
+
if block_given?
|
50
|
+
raise ::ArgumentError, 'both block and value argument given' unless default
|
51
|
+
|
52
|
+
each { |element| hash[element] = yield element }
|
53
|
+
elsif !default
|
54
|
+
each { |element| hash[element] = value }
|
55
|
+
else
|
56
|
+
return ::Hash[*flatten_once]
|
57
|
+
end
|
58
|
+
|
59
|
+
hash
|
60
|
+
end
|
61
|
+
|
62
|
+
alias_method :to_h, :to_hash
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
class Array
|
29
|
+
module VarianceMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# array.variance => aFloat
|
33
|
+
#
|
34
|
+
# Calculates the variance[http://en.wikipedia.org/wiki/Variance] of the
|
35
|
+
# values in _array_.
|
36
|
+
def variance
|
37
|
+
sx, sq = 0.0, 0.0
|
38
|
+
|
39
|
+
return sx if empty?
|
40
|
+
|
41
|
+
each { |x|
|
42
|
+
x = yield x if block_given?
|
43
|
+
|
44
|
+
sx += x
|
45
|
+
sq += x ** 2
|
46
|
+
}
|
47
|
+
|
48
|
+
(sq - sx ** 2 / size) / size
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :var, :variance
|
52
|
+
|
53
|
+
# call-seq:
|
54
|
+
# array.covariance => aFloat
|
55
|
+
#
|
56
|
+
# Calculates the covariance[http://en.wikipedia.org/wiki/Covariance] of
|
57
|
+
# the <tt>{x,y}</tt> pairs in _array_. If _array_ only contains values
|
58
|
+
# instead of pairs, +y+ will be the value and +x+ will be each value's
|
59
|
+
# position (rank) in _array_.
|
60
|
+
def covariance
|
61
|
+
sx, sy, sp = 0.0, 0.0, 0.0
|
62
|
+
|
63
|
+
return sx if empty?
|
64
|
+
|
65
|
+
target = first.respond_to?(:to_ary) ? self :
|
66
|
+
self.class.new(size) { |i| [i + 1, at(i)] }
|
67
|
+
|
68
|
+
target.each { |x, y|
|
69
|
+
sx += x
|
70
|
+
sy += y
|
71
|
+
sp += x * y
|
72
|
+
}
|
73
|
+
|
74
|
+
(sp - sx * sy / size) / size
|
75
|
+
end
|
76
|
+
|
77
|
+
alias_method :cov, :covariance
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'filemagic/ext'
|
29
|
+
rescue LoadError
|
30
|
+
def File.content_type(path) # :nodoc:
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'nuggets/uri/content_type'
|
37
|
+
rescue LoadError
|
38
|
+
module URI
|
39
|
+
def self.content_type(path) # :nodoc:
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
begin
|
46
|
+
require 'mime/types'
|
47
|
+
rescue LoadError
|
48
|
+
module MIME # :nodoc:
|
49
|
+
class Types # :nodoc:
|
50
|
+
def self.of(path)
|
51
|
+
[]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module Nuggets
|
58
|
+
module ContentType
|
59
|
+
|
60
|
+
extend self
|
61
|
+
|
62
|
+
# call-seq:
|
63
|
+
# ContentType.of(path) => aString or +nil+
|
64
|
+
#
|
65
|
+
# Get the MIME-Type of the file living at +path+. Either by looking
|
66
|
+
# directly into the file (requires FileMagic), or, assuming +path+
|
67
|
+
# might denote a URI, by asking the web server (via OpenURI), or
|
68
|
+
# finally by just looking at the file extension (requires MIME::Types).
|
69
|
+
# Returns +nil+ in case no decision could be made.
|
70
|
+
#
|
71
|
+
# NOTE: This is really only useful with the filemagic and mime-types gems
|
72
|
+
# installed.
|
73
|
+
def of(path)
|
74
|
+
::File.content_type(path) || ::URI.content_type(path) || (
|
75
|
+
t = ::MIME::Types.of(path).first and t.content_type
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Just a short-cut to make the code read nicer...
|
83
|
+
ContentType = ::Nuggets::ContentType
|
84
|
+
|
85
|
+
if $0 == __FILE__
|
86
|
+
[
|
87
|
+
__FILE__,
|
88
|
+
'bla/blub.jpg',
|
89
|
+
'bla/blub.blob',
|
90
|
+
'http://www.google.de',
|
91
|
+
'http://blackwinter.de/misc/ww.png',
|
92
|
+
'http://blackwinter.de/misc/ww.jpg',
|
93
|
+
'http://blackwinter.de/bla/blub.blob'
|
94
|
+
].each { |f|
|
95
|
+
p [f, ::ContentType.of(f)]
|
96
|
+
}
|
97
|
+
end
|