mug 0.2.4 → 0.2.5

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.
@@ -1,44 +1,44 @@
1
-
2
- module Math
3
- # The true circle constant.
4
- # The ratio of a circle's circumference to its radius.
5
- TAU = PI * 2.0
6
- end
7
-
8
- module BigMath
9
- ##
10
- # Computes the value of tau to the specific number of digits of precision.
11
- #
12
- # @param [Integer] prec the number of decimal digits of precision in the computed value.
13
- # @return [BigDecimal] the computed value
14
- # @raise [ArgumentError] if +prec+ is not positive
15
- #
16
- # @example
17
- # require 'bigdecimal'
18
- # require 'bigdecimal/math'
19
- # include BigMath
20
- #
21
- # puts TAU(150)
22
- #
23
- def TAU(prec)
24
- raise ArgumentError, 'Zero or negative argument for TAU' if prec <= 0
25
- PI(prec) * BigDecimal('2')
26
- end
27
- end
28
-
29
- =begin
30
- Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
31
-
32
- Permission to use, copy, modify, and/or distribute this software for any
33
- purpose with or without fee is hereby granted, provided that the above
34
- copyright notice and this permission notice appear in all copies.
35
-
36
- THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37
- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38
- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39
- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41
- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42
- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43
- =end
44
-
1
+
2
+ module Math
3
+ # The true circle constant.
4
+ # The ratio of a circle's circumference to its radius.
5
+ TAU = PI * 2.0
6
+ end
7
+
8
+ module BigMath
9
+ ##
10
+ # Computes the value of tau to the specific number of digits of precision.
11
+ #
12
+ # @param [Integer] prec the number of decimal digits of precision in the computed value.
13
+ # @return [BigDecimal] the computed value
14
+ # @raise [ArgumentError] if +prec+ is not positive
15
+ #
16
+ # @example
17
+ # require 'bigdecimal'
18
+ # require 'bigdecimal/math'
19
+ # include BigMath
20
+ #
21
+ # puts TAU(150)
22
+ #
23
+ def TAU(prec)
24
+ raise ArgumentError, 'Zero or negative argument for TAU' if prec <= 0
25
+ PI(prec) * BigDecimal('2')
26
+ end
27
+ end
28
+
29
+ =begin
30
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
31
+
32
+ Permission to use, copy, modify, and/or distribute this software for any
33
+ purpose with or without fee is hereby granted, provided that the above
34
+ copyright notice and this permission notice appear in all copies.
35
+
36
+ THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43
+ =end
44
+
@@ -0,0 +1,128 @@
1
+
2
+ module Enumerable
3
+
4
+ #
5
+ # Get the top +n+ items, in order from top to bottom.
6
+ #
7
+ # Returns an +Array+ even when +n+ is 1.
8
+ #
9
+ # @see Enumerable#sort
10
+ #
11
+ def top n=1, &blk
12
+ if block_given?
13
+ sort{|x,y| yield y, x }[0...n]
14
+ else
15
+ #top_by(n) {|x| x }
16
+ if n <= length
17
+ sort[-n..-1].reverse
18
+ else
19
+ sort.reverse
20
+ end
21
+ end
22
+ end
23
+
24
+ #
25
+ # Get the top +n+ items, in order from top to bottom, ordered
26
+ # by mapping the values through the given block.
27
+ #
28
+ # Returns an +Array+ even when +n+ is 1. Values that are tied
29
+ # after mapping are returned in the initial order.
30
+ #
31
+ # If no block is given, an enumerator is returned instead.
32
+ #
33
+ # @see Enumerable#sort_by
34
+ #
35
+ def top_by n=1, &blk
36
+ return enum_for(:top_by, n) unless block_given?
37
+ chain = {}
38
+ each do |x|
39
+ y = yield x
40
+ chain[y] ||= []
41
+ chain[y] << x
42
+ end
43
+ ary = []
44
+ chain.keys.sort.reverse.each do |k|
45
+ ary += chain[k]
46
+ break if ary.length > n
47
+ end
48
+ ary[0...n]
49
+ end
50
+
51
+ #
52
+ # Get the bottom +n+ items, ordered from bottom to top.
53
+ #
54
+ # Returns an +Array+ even when +n+ is 1.
55
+ #
56
+ # @see Enumerable#sort
57
+ #
58
+ def bottom n=1, &blk
59
+ if block_given?
60
+ sort(&blk)[0...n]
61
+ else
62
+ #bottom_by(n) {|x| x }
63
+ sort[0...n]
64
+ end
65
+ end
66
+
67
+ #
68
+ # Get the bottom +n+ items, in order from bottom to top, ordered
69
+ # by mapping the values through the given block.
70
+ #
71
+ # Returns an +Array+ even when +n+ is 1. Values that are tied
72
+ # after mapping are returned in the initial order.
73
+ #
74
+ # If no block is given, an enumerator is returned instead.
75
+ #
76
+ # @see Enumerable#sort_by
77
+ #
78
+ def bottom_by n=1, &blk
79
+ return enum_for(:bottom_by, n) unless block_given?
80
+ chain = {}
81
+ each do |x|
82
+ y = yield x
83
+ chain[y] ||= []
84
+ chain[y] << x
85
+ end
86
+ ary = []
87
+ chain.keys.sort.each do |k|
88
+ ary += chain[k]
89
+ break if ary.length > n
90
+ end
91
+ ary[0...n]
92
+ end
93
+
94
+ end
95
+
96
+ class Array
97
+ def top! n=1, &blk
98
+ replace(top n, &blk)
99
+ end
100
+ def top_by! n=1, &blk
101
+ return enum_for(:top_by!, n) unless block_given?
102
+ replace(top_by n, &blk)
103
+ end
104
+ def bottom! n=1, &blk
105
+ replace(bottom n, &blk)
106
+ end
107
+ def bottom_by! n=1, &blk
108
+ return enum_for(:bottom_by!, n) unless block_given?
109
+ replace(bottom_by n, &blk)
110
+ end
111
+ end
112
+
113
+ =begin
114
+ Copyright (c) 2014, Matthew Kerwin <matthew@kerwin.net.au>
115
+
116
+ Permission to use, copy, modify, and/or distribute this software for any
117
+ purpose with or without fee is hereby granted, provided that the above
118
+ copyright notice and this permission notice appear in all copies.
119
+
120
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
121
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
122
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
123
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
124
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
125
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
126
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
127
+ =end
128
+
@@ -1,41 +1,41 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- require_relative '../lib/mug/and-or'
5
- class Test_and_or < Test::Unit::TestCase
6
-
7
- def test_and
8
- assert_equal( 2, 1.and(2) )
9
- assert_equal( 2, 0.and(2) )
10
- assert_equal( 2, 'x'.and(2) )
11
- assert_equal( 2, true.and(2) )
12
- assert_equal( false, false.and(2) )
13
- assert_equal( nil, nil.and(2) )
14
- end
15
-
16
- def test_and_2
17
- assert_equal( [1], [1].and([2], &:empty?) )
18
- assert_equal( [2], [].and([2], &:empty?) )
19
- assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
20
- assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
21
- end
22
-
23
-
24
- def test_or
25
- assert_equal( 1, 1.or(2) )
26
- assert_equal( 0, 0.or(2) )
27
- assert_equal( 'x', 'x'.or(2) )
28
- assert_equal( true, true.or(2) )
29
- assert_equal( 2, false.or(2) )
30
- assert_equal( 2, nil.or(2) )
31
- end
32
-
33
- def test_or_2
34
- assert_equal( [2], [1].or([2], &:empty?) )
35
- assert_equal( [], [].or([2], &:empty?) )
36
- assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
37
- assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
38
- end
39
-
40
- end
41
-
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/and-or'
5
+ class Test_and_or < Test::Unit::TestCase
6
+
7
+ def test_and
8
+ assert_equal( 2, 1.and(2) )
9
+ assert_equal( 2, 0.and(2) )
10
+ assert_equal( 2, 'x'.and(2) )
11
+ assert_equal( 2, true.and(2) )
12
+ assert_equal( false, false.and(2) )
13
+ assert_equal( nil, nil.and(2) )
14
+ end
15
+
16
+ def test_and_2
17
+ assert_equal( [1], [1].and([2], &:empty?) )
18
+ assert_equal( [2], [].and([2], &:empty?) )
19
+ assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
20
+ assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
21
+ end
22
+
23
+
24
+ def test_or
25
+ assert_equal( 1, 1.or(2) )
26
+ assert_equal( 0, 0.or(2) )
27
+ assert_equal( 'x', 'x'.or(2) )
28
+ assert_equal( true, true.or(2) )
29
+ assert_equal( 2, false.or(2) )
30
+ assert_equal( 2, nil.or(2) )
31
+ end
32
+
33
+ def test_or_2
34
+ assert_equal( [2], [1].or([2], &:empty?) )
35
+ assert_equal( [], [].or([2], &:empty?) )
36
+ assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
37
+ assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
38
+ end
39
+
40
+ end
41
+
@@ -1,65 +1,65 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- module FMCTest
5
- class A
6
- def initialize; @b = B.new; end
7
- attr_accessor :b
8
- end
9
-
10
- class B
11
- def initialize; @c = C.new; end
12
- attr_accessor :c
13
- end
14
-
15
- class C
16
- def to_i; 1; end
17
- end
18
- end
19
-
20
- def false.c
21
- 2
22
- end
23
-
24
- require_relative '../lib/mug/fragile-method-chain'
25
- class Test_fmc < Test::Unit::TestCase
26
- def test_fmc_nil
27
- a = FMCTest::A.new
28
- assert_equal( 1, a._?.b.c.to_i._! )
29
- assert_equal( 1, a._?.b.c._!.to_i )
30
- assert_equal( 1, a._?.b._!.c.to_i )
31
- a.b.c = nil
32
- assert_nil( a._?.b.c.to_i._! )
33
- assert_equal( 0, a._?.b.c._!.to_i )
34
- assert_equal( 0, a._?.b._!.c.to_i )
35
- a.b = nil
36
- assert_nil( a._?.b.c.to_i._! )
37
- assert_equal( 0, a._?.b.c._!.to_i )
38
- assert_raise(NoMethodError) { a._?.b._!.c.to_i }
39
- a = nil
40
- assert_nil( a._?.b.c.to_i._! )
41
- end
42
- def test_fmc_false
43
- a = FMCTest::A.new
44
- assert_equal( 1, a._?.b.c.to_i._! )
45
- assert_equal( 1, a._?.b.c._!.to_i )
46
- assert_equal( 1, a._?.b._!.c.to_i )
47
- a.b.c = false
48
- assert_equal( false, a._?.b.c.to_i._! )
49
- assert_raise(NoMethodError) { a._?.b.c._!.to_i }
50
- assert_raise(NoMethodError) { a._?.b._!.c.to_i }
51
- a.b = false
52
- assert_equal( false, a._?.b.c.to_i._! )
53
- assert_raise(NoMethodError) { a._?.b.c._!.to_i }
54
- assert_equal( 2, a._?.b._!.c.to_i )
55
- a = false
56
- assert_equal( false, a._?.b.c.to_i._! )
57
- assert_raise(NoMethodError) { a._?.b.c._!.to_i }
58
- assert_equal( 2, a._?.b._!.c.to_i )
59
- end
60
- def test_fmc_nested
61
- a = FMCTest::A.new
62
- assert_equal( 1, a._?.b._?.c._!.to_i._! )
63
- end
64
- end
65
-
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ module FMCTest
5
+ class A
6
+ def initialize; @b = B.new; end
7
+ attr_accessor :b
8
+ end
9
+
10
+ class B
11
+ def initialize; @c = C.new; end
12
+ attr_accessor :c
13
+ end
14
+
15
+ class C
16
+ def to_i; 1; end
17
+ end
18
+ end
19
+
20
+ def false.c
21
+ 2
22
+ end
23
+
24
+ require_relative '../lib/mug/fragile-method-chain'
25
+ class Test_fmc < Test::Unit::TestCase
26
+ def test_fmc_nil
27
+ a = FMCTest::A.new
28
+ assert_equal( 1, a._?.b.c.to_i._! )
29
+ assert_equal( 1, a._?.b.c._!.to_i )
30
+ assert_equal( 1, a._?.b._!.c.to_i )
31
+ a.b.c = nil
32
+ assert_nil( a._?.b.c.to_i._! )
33
+ assert_equal( 0, a._?.b.c._!.to_i )
34
+ assert_equal( 0, a._?.b._!.c.to_i )
35
+ a.b = nil
36
+ assert_nil( a._?.b.c.to_i._! )
37
+ assert_equal( 0, a._?.b.c._!.to_i )
38
+ assert_raise(NoMethodError) { a._?.b._!.c.to_i }
39
+ a = nil
40
+ assert_nil( a._?.b.c.to_i._! )
41
+ end
42
+ def test_fmc_false
43
+ a = FMCTest::A.new
44
+ assert_equal( 1, a._?.b.c.to_i._! )
45
+ assert_equal( 1, a._?.b.c._!.to_i )
46
+ assert_equal( 1, a._?.b._!.c.to_i )
47
+ a.b.c = false
48
+ assert_equal( false, a._?.b.c.to_i._! )
49
+ assert_raise(NoMethodError) { a._?.b.c._!.to_i }
50
+ assert_raise(NoMethodError) { a._?.b._!.c.to_i }
51
+ a.b = false
52
+ assert_equal( false, a._?.b.c.to_i._! )
53
+ assert_raise(NoMethodError) { a._?.b.c._!.to_i }
54
+ assert_equal( 2, a._?.b._!.c.to_i )
55
+ a = false
56
+ assert_equal( false, a._?.b.c.to_i._! )
57
+ assert_raise(NoMethodError) { a._?.b.c._!.to_i }
58
+ assert_equal( 2, a._?.b._!.c.to_i )
59
+ end
60
+ def test_fmc_nested
61
+ a = FMCTest::A.new
62
+ assert_equal( 1, a._?.b._?.c._!.to_i._! )
63
+ end
64
+ end
65
+
@@ -1,18 +1,18 @@
1
- require 'test/unit'
2
- $VERBOSE = true
3
-
4
- require_relative '../lib/mug/iterator/for'
5
- class IterForTest < Test::Unit::TestCase
6
- def test_iter_for
7
- # Test on Integer#next => Integer
8
- assert_equal([0, 1, 2, 3, 4], 0.iter_for(:next).take(5))
9
- # Test on String#succ => String
10
- assert_equal(%w[a b c d e f g h i j], 'a'.iter_for(:succ).take(10))
11
- # Test on Integer#inspect => String#inspect => String
12
- assert_equal([1, "1", "\"1\""], 1.iter_for(:inspect).take(3))
13
- end
14
- def test_iter_for_args
15
- # Test with a parameter
16
- assert_equal([0, 2, 4, 6, 8], 0.iter_for(:+, 2).take(5))
17
- end
18
- end
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/iterator/for'
5
+ class IterForTest < Test::Unit::TestCase
6
+ def test_iter_for
7
+ # Test on Integer#next => Integer
8
+ assert_equal([0, 1, 2, 3, 4], 0.iter_for(:next).take(5))
9
+ # Test on String#succ => String
10
+ assert_equal(%w[a b c d e f g h i j], 'a'.iter_for(:succ).take(10))
11
+ # Test on Integer#inspect => String#inspect => String
12
+ assert_equal([1, "1", "\"1\""], 1.iter_for(:inspect).take(3))
13
+ end
14
+ def test_iter_for_args
15
+ # Test with a parameter
16
+ assert_equal([0, 2, 4, 6, 8], 0.iter_for(:+, 2).take(5))
17
+ end
18
+ end