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,50 @@
|
|
1
|
+
require 'nuggets/array/boost'
|
2
|
+
|
3
|
+
describe_extended Array, Nuggets::Array::BoostMixin do
|
4
|
+
|
5
|
+
example {
|
6
|
+
lambda { [].boost_factor([]) }.should raise_error(NoMethodError)
|
7
|
+
}
|
8
|
+
|
9
|
+
example {
|
10
|
+
lambda { [1].boost_factor([]) }.should raise_error(NoMethodError)
|
11
|
+
}
|
12
|
+
|
13
|
+
example {
|
14
|
+
lambda { [].boost_factor([1]) }.should raise_error(TypeError)
|
15
|
+
}
|
16
|
+
|
17
|
+
example {
|
18
|
+
a, b = [0].boof([0]), Float::NAN
|
19
|
+
|
20
|
+
a.class.should == b.class
|
21
|
+
a.to_s.should == b.to_s
|
22
|
+
}
|
23
|
+
|
24
|
+
example {
|
25
|
+
1.step(100, 10) { |i| [i].boof([0]).should == -1 }
|
26
|
+
}
|
27
|
+
|
28
|
+
example {
|
29
|
+
1.step(100, 10) { |i| [0].boof([i]).should == Float::INFINITY }
|
30
|
+
}
|
31
|
+
|
32
|
+
example {
|
33
|
+
a, b = [1.5, 1.6, 1.4], [0.7, 0.8, 0.8]
|
34
|
+
|
35
|
+
a.boof(b).should equal_float(-0.48888888888888893)
|
36
|
+
a.boof(b, &:max).should equal_float(-0.5)
|
37
|
+
a.boof(b, &:geomean).should equal_float(-0.4891270450709646)
|
38
|
+
a.boof(b) { |x| x.inject(:-) }.should equal_float(-0.3999999999999999)
|
39
|
+
}
|
40
|
+
|
41
|
+
[
|
42
|
+
[[1], [1], :mean],
|
43
|
+
[[1], [1, 2, 3], :mean],
|
44
|
+
[[1.5, 1.6, 1.4], [0.7, 0.8, 0.8], :mean],
|
45
|
+
[[1.5, 1.6, 1.4], [0.7, 0.8, 0.8], :geomean]
|
46
|
+
].each { |a, b, m|
|
47
|
+
example { a.boost(a.boof(b, &m)).send(m).should equal_float(b.send(m)) }
|
48
|
+
}
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'nuggets/array/combination'
|
2
|
+
|
3
|
+
describe Array, 'combination' do
|
4
|
+
|
5
|
+
let(:a) { %w[a b c d] }
|
6
|
+
|
7
|
+
example {
|
8
|
+
r = [%w[a b c], %w[a b d], %w[a c d], %w[b c d]]
|
9
|
+
|
10
|
+
a.comb(3).should == r
|
11
|
+
lambda { |b| a.comb(3, &b) }.should yield_successive_args(*r)
|
12
|
+
}
|
13
|
+
|
14
|
+
example {
|
15
|
+
a.comb(4, 2, 4).should == [%w[a b c d], %w[a b], %w[a c], %w[a d], %w[b c], %w[b d], %w[c d], %w[a b c d]]
|
16
|
+
}
|
17
|
+
|
18
|
+
example {
|
19
|
+
r = [%w[a b c d], %w[a b c], %w[a b d], %w[a c d], %w[b c d], %w[a b], %w[a c], %w[a d], %w[b c], %w[b d], %w[c d], %w[a], %w[b], %w[c], %w[d], []]
|
20
|
+
|
21
|
+
a.comb.should == r
|
22
|
+
lambda { |b| a.comb(&b) }.should yield_successive_args(*r)
|
23
|
+
}
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'nuggets/array/correlation'
|
2
|
+
|
3
|
+
describe_extended Array, Nuggets::Array::CorrelationMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
[].correlation_coefficient.should == 0.0
|
7
|
+
end
|
8
|
+
|
9
|
+
example do
|
10
|
+
[1].correlation_coefficient.should == 1.0
|
11
|
+
end
|
12
|
+
|
13
|
+
example do
|
14
|
+
[1, 1, 1].correlation_coefficient.should == 1.0
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
[1, 2, 3].correlation_coefficient.should == 1.0
|
19
|
+
end
|
20
|
+
|
21
|
+
example do
|
22
|
+
[-1, -2, -3].correlation_coefficient.should == -1.0
|
23
|
+
end
|
24
|
+
|
25
|
+
example do
|
26
|
+
[1, 1.5, 2].correlation_coefficient.should == 1.0
|
27
|
+
end
|
28
|
+
|
29
|
+
example do
|
30
|
+
[3, 2, 1].correlation_coefficient.should == -1.0
|
31
|
+
end
|
32
|
+
|
33
|
+
example do
|
34
|
+
[-3, -2, -1].correlation_coefficient.should == -[3, 2, 1].correlation_coefficient
|
35
|
+
end
|
36
|
+
|
37
|
+
example do
|
38
|
+
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].correlation_coefficient.should equal_float(0.478471417102228)
|
39
|
+
end
|
40
|
+
|
41
|
+
example do
|
42
|
+
[[1, 1]].correlation_coefficient.should == 1.0
|
43
|
+
end
|
44
|
+
|
45
|
+
example do
|
46
|
+
[[1, 1], [1, 1], [1, 1]].correlation_coefficient.should == 1.0
|
47
|
+
end
|
48
|
+
|
49
|
+
example do
|
50
|
+
[[-1, 1], [-1, 1], [-1, 1]].correlation_coefficient.should == 1.0
|
51
|
+
end
|
52
|
+
|
53
|
+
example do
|
54
|
+
[[1, -1], [1, -1], [1, -1]].correlation_coefficient.should == 1.0
|
55
|
+
end
|
56
|
+
|
57
|
+
example do
|
58
|
+
[[1, 1], [1, 2], [1, 3]].correlation_coefficient.should == 1.0
|
59
|
+
end
|
60
|
+
|
61
|
+
example do
|
62
|
+
[[1, 1], [2, 1], [3, 1]].correlation_coefficient.should == 1.0
|
63
|
+
end
|
64
|
+
|
65
|
+
example do
|
66
|
+
[[-1, 1], [-2, 1], [-3, 1]].correlation_coefficient.should == 1.0
|
67
|
+
end
|
68
|
+
|
69
|
+
example do
|
70
|
+
[[1, -1], [2, -1], [3, -1]].correlation_coefficient.should == 1.0
|
71
|
+
end
|
72
|
+
|
73
|
+
example do
|
74
|
+
[[3, 1], [2, 2], [1, 3]].correlation_coefficient.should == -1.0
|
75
|
+
end
|
76
|
+
|
77
|
+
example do
|
78
|
+
[[-3, 3], [-2, 2], [-1, 1]].correlation_coefficient.should == -[[3, 3], [2, 2], [1, 1]].correlation_coefficient
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'nuggets/array/flatten_once'
|
2
|
+
|
3
|
+
describe Array, 'flatten_once' do
|
4
|
+
|
5
|
+
let(:a) { [1, 2, [3, 4, 5], 6, [7, [8, 9]]] }
|
6
|
+
|
7
|
+
example {
|
8
|
+
a.flatten_once.should == [1, 2, 3, 4, 5, 6, 7, [8, 9]]
|
9
|
+
}
|
10
|
+
|
11
|
+
example {
|
12
|
+
a.flatten_once!
|
13
|
+
a.should == [1, 2, 3, 4, 5, 6, 7, [8, 9]]
|
14
|
+
}
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'nuggets/array/flush'
|
2
|
+
|
3
|
+
describe_extended Array, Nuggets::Array::FlushMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
a = [1, 2, 3, 4]
|
7
|
+
a.flush.should == [1, 2, 3, 4]
|
8
|
+
a.should be_empty
|
9
|
+
end
|
10
|
+
|
11
|
+
example do
|
12
|
+
a = [1, 2, 3, 4]
|
13
|
+
a.flush.push(42).should == [1, 2, 3, 4, 42]
|
14
|
+
a.should be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
a = [1, 2, 3, 4]
|
19
|
+
a.flush { |b| b.should == [1, 2, 3, 4]; 42 }.should == 42
|
20
|
+
a.should be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
example do
|
24
|
+
a = [1, 2, 3, 4]
|
25
|
+
a.flush { |b| b }.should be_empty
|
26
|
+
a.should be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
example do
|
30
|
+
a, c = [1, 2, 3, 4], nil
|
31
|
+
a.flush { |b| c = b; c.should == [1, 2, 3, 4] }
|
32
|
+
a.should be_empty
|
33
|
+
c.should be_empty
|
34
|
+
end
|
35
|
+
|
36
|
+
example do
|
37
|
+
a, c = [1, 2, 3, 4], nil
|
38
|
+
a.flush { |b| c = b.dup; c.should == [1, 2, 3, 4] }
|
39
|
+
a.should be_empty
|
40
|
+
c.should == [1, 2, 3, 4]
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'nuggets/array/format'
|
2
|
+
|
3
|
+
describe Array, 'format' do
|
4
|
+
|
5
|
+
def self.test_format(*f)
|
6
|
+
f.pop.each { |x, y| example { (f.dup % x).should == y } }
|
7
|
+
end
|
8
|
+
|
9
|
+
test_format('"%s"',
|
10
|
+
[] => '',
|
11
|
+
'string' => '"string"',
|
12
|
+
'' => ''
|
13
|
+
)
|
14
|
+
|
15
|
+
test_format('%s, (%s)', '%s', '(%s)',
|
16
|
+
['place', 'country'] => 'place, (country)',
|
17
|
+
['place', '' ] => 'place',
|
18
|
+
['', 'country'] => '(country)',
|
19
|
+
['', '' ] => ''
|
20
|
+
)
|
21
|
+
|
22
|
+
test_format('%s: %s (%s)', '%s: %s', '%s (%s)', '%s (%s)',
|
23
|
+
['author', 'title', 'year'] => 'author: title (year)',
|
24
|
+
['author', 'title', '' ] => 'author: title',
|
25
|
+
['author', '', 'year'] => 'author (year)',
|
26
|
+
['', 'title', 'year'] => 'title (year)',
|
27
|
+
['author', '' , '' ] => 'author',
|
28
|
+
['', 'title', '' ] => 'title',
|
29
|
+
['', '', 'year'] => 'year',
|
30
|
+
['', '', '' ] => ''
|
31
|
+
)
|
32
|
+
|
33
|
+
test_format({ :sep => ':' },
|
34
|
+
['1', '2', '3', '4'] => '1:2:3:4',
|
35
|
+
['1', '2', '3', '' ] => '1:2:3',
|
36
|
+
['1', '2', '', '4'] => '1:2:4',
|
37
|
+
['1', '', '3', '4'] => '1:3:4',
|
38
|
+
['', '2', '3', '4'] => '2:3:4',
|
39
|
+
['1', '2', '', '' ] => '1:2',
|
40
|
+
['1', '', '3', '' ] => '1:3',
|
41
|
+
['1', '', '', '4'] => '1:4',
|
42
|
+
['', '2', '3', '' ] => '2:3',
|
43
|
+
['', '2', '', '4'] => '2:4',
|
44
|
+
['', '', '3', '4'] => '3:4',
|
45
|
+
['1', '', '', '' ] => '1',
|
46
|
+
['', '2', '', '' ] => '2',
|
47
|
+
['', '', '3', '' ] => '3',
|
48
|
+
['', '', '', '4'] => '4',
|
49
|
+
['', '', '', '' ] => ''
|
50
|
+
)
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'nuggets/array/hashify'
|
2
|
+
|
3
|
+
describe_extended Array, Nuggets::Array::HashifyMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
hash = [1, 2, 3, 4].hashify
|
7
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
8
|
+
hash[42].should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
example do
|
12
|
+
[1, 2, 3, 4].hashify { |i| [i, i ** 2] }.should == { 1 => 1, 2 => 4, 3 => 9, 4 => 16 }
|
13
|
+
end
|
14
|
+
|
15
|
+
example do
|
16
|
+
[1, 2, 3, 4].hashify { |i| [i ** 2, i] }.should == { 1 => 1, 4 => 2, 9 => 3, 16 => 4 }
|
17
|
+
end
|
18
|
+
|
19
|
+
example do
|
20
|
+
lambda { [1, 2, 3, 4].hashify { |i| i } }.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
|
23
|
+
example do
|
24
|
+
hash = [1, 2, 3, 4].hashify(0)
|
25
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
26
|
+
hash[42].should == 0
|
27
|
+
end
|
28
|
+
|
29
|
+
example do
|
30
|
+
hash = [1, 2, 3, 4].hashify(lambda { |h, k| h[k] = [] })
|
31
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
|
32
|
+
hash[42].should == []
|
33
|
+
end
|
34
|
+
|
35
|
+
example do
|
36
|
+
hash = [1, 2, 3, 4].hashify(4 => -1, 23 => 42)
|
37
|
+
hash.should == { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 23 => 42 }
|
38
|
+
hash[42].should be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'nuggets/array/histogram'
|
2
|
+
|
3
|
+
describe_extended Array, Nuggets::Array::HistogramMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
[].histogram.should == {}
|
7
|
+
end
|
8
|
+
|
9
|
+
example do
|
10
|
+
[1].histogram.should == { 1 => 1 }
|
11
|
+
end
|
12
|
+
|
13
|
+
example do
|
14
|
+
[1, 1, 1].histogram.should == { 1 => 3 }
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
[1, 2, 2, 3].histogram.should == { 1 => 1, 2 => 2, 3 => 1 }
|
19
|
+
end
|
20
|
+
|
21
|
+
example do
|
22
|
+
[3, 2, 2, 1].histogram.should == [1, 2, 2, 3].histogram
|
23
|
+
end
|
24
|
+
|
25
|
+
example do
|
26
|
+
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].histogram.should == {
|
27
|
+
-4 => 1, -2 => 1, 0 => 2, 1 => 4, 2 => 2, 3 => 2, 24 => 1
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
example do
|
32
|
+
%w[one two three three].histogram.should == { 'one' => 1, 'two' => 1, 'three' => 2 }
|
33
|
+
end
|
34
|
+
|
35
|
+
example do
|
36
|
+
%w[three two one three].histogram.should == %w[one two three three].histogram
|
37
|
+
end
|
38
|
+
|
39
|
+
example do
|
40
|
+
[1, 2, 2, 3, 4].histogram { |x| x * 2 }.should == { 2 => 1, 4 => 2, 6 => 1, 8 => 1 }
|
41
|
+
end
|
42
|
+
|
43
|
+
example do
|
44
|
+
[1, 2, 3, 4].histogram { |x| x / 2 }.should == { 0 => 1, 1 => 2, 2 => 1 }
|
45
|
+
end
|
46
|
+
|
47
|
+
example do
|
48
|
+
[].probability_mass_function.should == {}
|
49
|
+
end
|
50
|
+
|
51
|
+
example do
|
52
|
+
[1].probability_mass_function.should == { 1 => 1 }
|
53
|
+
end
|
54
|
+
|
55
|
+
example do
|
56
|
+
[1, 1, 1].probability_mass_function.should == { 1 => 1 }
|
57
|
+
end
|
58
|
+
|
59
|
+
example do
|
60
|
+
[1, 2, 2, 3].probability_mass_function.should == { 1 => 0.25, 2 => 0.5, 3 => 0.25 }
|
61
|
+
end
|
62
|
+
|
63
|
+
example do
|
64
|
+
[3, 2, 2, 1].probability_mass_function.should == [1, 2, 2, 3].probability_mass_function
|
65
|
+
end
|
66
|
+
|
67
|
+
example do
|
68
|
+
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].probability_mass_function.values.inject(:+).should equal_float(1.0)
|
69
|
+
end
|
70
|
+
|
71
|
+
example do
|
72
|
+
%w[one two three three].probability_mass_function.should == { 'one' => 0.25, 'two' => 0.25, 'three' => 0.5 }
|
73
|
+
end
|
74
|
+
|
75
|
+
example do
|
76
|
+
%w[three two one three].probability_mass_function.should == %w[one two three three].probability_mass_function
|
77
|
+
end
|
78
|
+
|
79
|
+
example do
|
80
|
+
[1, 2, 2, 3, 4].probability_mass_function { |x| x * 2 }.should == { 2 => 0.2, 4 => 0.4, 6 => 0.2, 8 => 0.2 }
|
81
|
+
end
|
82
|
+
|
83
|
+
example do
|
84
|
+
[1, 2, 3, 4].probability_mass_function { |x| x / 2 }.should == { 0 => 0.25, 1 => 0.5, 2 => 0.25 }
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'nuggets/array/in_order'
|
2
|
+
|
3
|
+
describe Array, 'in_order' do
|
4
|
+
|
5
|
+
let(:a) { [:created_at, :email, :login, :updated_at] }
|
6
|
+
|
7
|
+
example { a.in_order(:login, :email).should == [:login, :email, :created_at, :updated_at] }
|
8
|
+
|
9
|
+
example { a.in_order(:email, :address).should == [:email, :created_at, :login, :updated_at] }
|
10
|
+
|
11
|
+
example { a.in_order!(:login, :email); a.should == [:login, :email, :created_at, :updated_at] }
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'nuggets/array/limit'
|
2
|
+
require 'nuggets/numeric/limit'
|
3
|
+
|
4
|
+
describe_extended Array, Nuggets::Array::LimitMixin do
|
5
|
+
|
6
|
+
describe '#limit' do
|
7
|
+
|
8
|
+
example do
|
9
|
+
[].limit(0, 1).should == []
|
10
|
+
end
|
11
|
+
|
12
|
+
example do
|
13
|
+
[1].limit(0, 1).should == [1]
|
14
|
+
end
|
15
|
+
|
16
|
+
example do
|
17
|
+
[1, 1, 1].limit(0, 1).should == [1]
|
18
|
+
end
|
19
|
+
|
20
|
+
example do
|
21
|
+
[1, 2, 3].limit(0, 1).should == [1]
|
22
|
+
end
|
23
|
+
|
24
|
+
example do
|
25
|
+
[1, 2, 3].limit(0, 2).should == [1, 2]
|
26
|
+
end
|
27
|
+
|
28
|
+
example do
|
29
|
+
[-3, -2, -1].limit(0, 1).should == [0]
|
30
|
+
end
|
31
|
+
|
32
|
+
example do
|
33
|
+
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].limit(-2, 2).should == [1, -2, 2, 0]
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#cap' do
|
39
|
+
|
40
|
+
example do
|
41
|
+
[1, 1, 1].cap(1).should == [1, 1, 1]
|
42
|
+
end
|
43
|
+
|
44
|
+
example do
|
45
|
+
[1, 2, 3].cap(1).should == [1, 1, 1]
|
46
|
+
end
|
47
|
+
|
48
|
+
example do
|
49
|
+
[1, 2, 3].cap(2).should == [1, 2, 2]
|
50
|
+
end
|
51
|
+
|
52
|
+
example do
|
53
|
+
[-3, -2, -1].cap(0).should == [-3, -2, -1]
|
54
|
+
end
|
55
|
+
|
56
|
+
example do
|
57
|
+
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].cap(-2..2).should == [1, -2, 1, 2, 2, -2, 0, 2, 1, 2, 1, 0, 2]
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|