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