mug 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 176afb1c7d977358227e76adffe6aa5651790298
4
- data.tar.gz: 53f517bf29dcdf3ada2b4cdfede2d250b17e97cb
3
+ metadata.gz: f3ad7fb03114e7062b93539759918f1ebbe542a9
4
+ data.tar.gz: 35ffe2f0573509eced89ea9e2b73efb991cbd619
5
5
  SHA512:
6
- metadata.gz: 60af159ec430300b58fae685df79eaeda031499c9b0f6ab092301edcec48e5560bb64ba2bd713130380266a9011449dc4946c8bd512aaed9d7099c2f96217a72
7
- data.tar.gz: 654b5b73f1da2c2642c01ee5c75569c9aeeafb2cb86ca70c108746ff9dc6a99277fdfe1c1c4909f7d460477f5bcd8519cba8e06c32374a444d2e1f5efadf0c17
6
+ metadata.gz: be694ddef15890b59a471dcea87d24f1b9df9da15c89d0d69c4c1c3786bd3f3c0bff56b876b06ecad0e4c439552610c81d2be46cd527a494b16749ffb7ff7510
7
+ data.tar.gz: 524a47ce38c90eabea52a6659154cbe08869a5f59b1ba6230f0b17d48c0fd9e1f6ccb5739d95fd6e8edd5f0227365644a07647039691fc072c01a311077c7ba8
data/lib/mug/bool.rb CHANGED
@@ -110,3 +110,19 @@ class Exception
110
110
  end
111
111
  end
112
112
 
113
+ =begin
114
+ Copyright (c) 2013, 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
+
@@ -51,3 +51,20 @@ class Object
51
51
  FragileMethodChain.new(self)
52
52
  end
53
53
  end
54
+
55
+ =begin
56
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
57
+
58
+ Permission to use, copy, modify, and/or distribute this software for any
59
+ purpose with or without fee is hereby granted, provided that the above
60
+ copyright notice and this permission notice appear in all copies.
61
+
62
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
63
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
65
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
68
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69
+ =end
70
+
@@ -0,0 +1,100 @@
1
+
2
+ class Hash
3
+ #
4
+ # Returns a new hash which is a copy of the current hash but each
5
+ # value is replaced by the result of running it through +block+.
6
+ #
7
+ # {'a'=>1, 'b'=>2}.map_values { |v| v*2 } #=> {'a'=>2, 'b'=>4}
8
+ # {'a'=>1, 'b'=>2}.map_values { "cat" } #=> {'a'=>"cat", 'b'=>"cat"}
9
+ #
10
+ def map_values &block # :yields: value
11
+ hsh = {}
12
+ each do |k, v|
13
+ hsh[k] = yield v
14
+ end
15
+ hsh
16
+ end
17
+
18
+ #
19
+ # Replaces the values in +hsh+ by running them each through +block+.
20
+ #
21
+ # See: #map_values
22
+ #
23
+ def map_values! &block # :yields: value
24
+ replace map_values(&block)
25
+ end
26
+
27
+ #
28
+ # Returns a new hash which is a copy of the current hash but each
29
+ # key is replaced by the result of running it through +block+.
30
+ #
31
+ # If +block+ returns duplicate keys, they will be overwritten in
32
+ # the resulting hash.
33
+ #
34
+ # {'a'=>1, 'b'=>2}.map_keys { |k| k*2 } #=> {'aa'=>1, 'bb'=>2}
35
+ # {'a'=>1, 'b'=>2}.map_keys { "cat" } #=> {'cat'=>2}
36
+ #
37
+ def map_keys &block # :yields: key
38
+ hsh = {}
39
+ each do |k, v|
40
+ hsh[ yield k ] = v
41
+ end
42
+ hsh
43
+ end
44
+
45
+ #
46
+ # Replaces the keys in +hsh+ by running them each through +block+.
47
+ #
48
+ # If +block+ returns duplicate keys, they will be overwritten in turn.
49
+ #
50
+ # See: #map_keys
51
+ #
52
+ def map_keys! &block # :yields: key
53
+ replace map_keys(&block)
54
+ end
55
+
56
+ #
57
+ # Returns a new hash which is a copy of the current hash but each
58
+ # key-value pair is replaced by the result of running it through +block+.
59
+ #
60
+ # If +block+ returns duplicate keys, they will be overwritten in
61
+ # the resulting hash.
62
+ #
63
+ # {'a'=>1, 'b'=>2}.map_pairs { |k,v| [k*2, v+1] } #=> {'aa'=>2, 'bb'=>3}
64
+ # {'a'=>1, 'b'=>2}.map_pairs { ["cat","dog"] } #=> {'cat'=>'dog'}
65
+ #
66
+ def map_pairs &block # :yields: key, value
67
+ hsh = {}
68
+ each do |k, v|
69
+ a, b = yield k, v
70
+ hsh[a] = b
71
+ end
72
+ hsh
73
+ end
74
+
75
+ #
76
+ # Replaces the values in +hsh+ by running them each through +block+.
77
+ #
78
+ # See: #map_values
79
+ #
80
+ def map_pairs! &block # :yields: key, value
81
+ replace map_pairs(&block)
82
+ end
83
+ end
84
+
85
+ =begin
86
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
87
+
88
+ Permission to use, copy, modify, and/or distribute this software for any
89
+ purpose with or without fee is hereby granted, provided that the above
90
+ copyright notice and this permission notice appear in all copies.
91
+
92
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
93
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
94
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
95
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
96
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
97
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
98
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
99
+ =end
100
+
@@ -10,3 +10,20 @@ class Object
10
10
  end
11
11
  alias :to_iter :iter_for
12
12
  end
13
+
14
+ =begin
15
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
16
+
17
+ Permission to use, copy, modify, and/or distribute this software for any
18
+ purpose with or without fee is hereby granted, provided that the above
19
+ copyright notice and this permission notice appear in all copies.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
22
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
23
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
24
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
+ =end
29
+
@@ -10,3 +10,20 @@ class Method
10
10
  Iterator.new receiver, name, *args
11
11
  end
12
12
  end
13
+
14
+ =begin
15
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
16
+
17
+ Permission to use, copy, modify, and/or distribute this software for any
18
+ purpose with or without fee is hereby granted, provided that the above
19
+ copyright notice and this permission notice appear in all copies.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
22
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
23
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
24
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
+ =end
29
+
@@ -25,3 +25,20 @@ class Iterator < Enumerator
25
25
  end
26
26
  end
27
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
+
data/lib/mug/maybe.rb CHANGED
@@ -40,3 +40,20 @@ class Object
40
40
  end
41
41
  end
42
42
  end
43
+
44
+ =begin
45
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
46
+
47
+ Permission to use, copy, modify, and/or distribute this software for any
48
+ purpose with or without fee is hereby granted, provided that the above
49
+ copyright notice and this permission notice appear in all copies.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
52
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
53
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
54
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
55
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
56
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
57
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
58
+ =end
59
+
data/lib/mug/self.rb CHANGED
@@ -14,3 +14,20 @@ class Object
14
14
  end
15
15
  end
16
16
  end
17
+
18
+ =begin
19
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
20
+
21
+ Permission to use, copy, modify, and/or distribute this software for any
22
+ purpose with or without fee is hereby granted, provided that the above
23
+ copyright notice and this permission notice appear in all copies.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
26
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
27
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
28
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32
+ =end
33
+
data/lib/mug/to_h.rb ADDED
@@ -0,0 +1,44 @@
1
+
2
+ unless {}.respond_to? :to_h
3
+ warn %|Warning: "mug/to_h" does not make much sense without "to_h" (https://rubygems.org/gems/to_h)|
4
+ end
5
+
6
+ module Enumerable
7
+ #
8
+ # Converts +enum+ to a Hash.
9
+ #
10
+ # Each element of +enum+ must be a single item, or an array of two items.
11
+ # Duplicate keys are overwritten in order.
12
+ #
13
+ # [].to_h #=> {}
14
+ # [1,2].to_h #=> {1=>nil, 2=>nil}
15
+ # (1..2).to_h #=> {1=>nil, 2=>nil}
16
+ # [[1,2],[3,4]].to_h #=> {1=>2, 3=>4}
17
+ # [[1,2],[1,4]].to_h #=> {1=>4}
18
+ #
19
+ def to_h
20
+ hsh = {}
21
+ each do |k,v,*x|
22
+ raise ArgumentError, "invalid number of elements (#{x.length+1} for 1..2)" if x.any?
23
+ hsh[k] = v
24
+ end
25
+ hsh
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
+
data/lib/mug.rb CHANGED
@@ -1,7 +1,10 @@
1
1
 
2
2
  require_relative 'mug/bool'
3
+ require_relative 'mug/hashmap'
3
4
  require_relative 'mug/fragile-method-chain'
4
5
  require_relative 'mug/iterator/for'
5
6
  require_relative 'mug/iterator/method'
6
7
  require_relative 'mug/maybe'
7
8
  require_relative 'mug/self'
9
+ require_relative 'mug/to_h'
10
+
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ if RUBY_VERSION.to_f < 1.9
5
+ class Symbol
6
+ def next
7
+ to_s.next.to_sym
8
+ end
9
+ end
10
+ end
11
+
12
+ require_relative '../lib/mug/hashmap'
13
+ class Test_hashmap < Test::Unit::TestCase
14
+ def test_hashmap
15
+ h = {'s'=>1, :x=>2, 3=>'a'}
16
+ h_k_next = {'t'=>1, :y=>2, 4=>'a'}
17
+ h_v_next = {'s'=>2, :x=>3, 3=>'b'}
18
+ h_p_next = {'t'=>2, :y=>3, 4=>'b'}
19
+ assert_equal( h_k_next, h.map_keys {|k| k.next } )
20
+ assert_equal( h_v_next, h.map_values{|v| v.next } )
21
+ assert_equal( h_p_next, h.map_pairs {|k,v| [k.next,v.next] } )
22
+ end
23
+ end
24
+
data/test/test-to_h.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+
3
+ $VERBOSE = true
4
+ require_relative '../lib/mug/to_h'
5
+ class Test_enum_to_h < Test::Unit::TestCase
6
+ def test_enum_to_h
7
+ assert_equal( {}, [].to_h )
8
+ assert_equal( {1=>nil,2=>nil}, [1,2].to_h )
9
+ assert_equal( {1=>nil,2=>nil}, (1..2).to_h )
10
+ assert_equal( {1=>2,3=>4}, [[1,2],[3,4]].to_h )
11
+ assert_equal( {1=>4}, [[1,2],[1,4]].to_h )
12
+ assert_raise(ArgumentError) { [[1,2,3]].to_h }
13
+ end
14
+ end
15
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
@@ -21,6 +21,8 @@ extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - lib/mug.rb
24
+ - lib/mug/hashmap.rb
25
+ - lib/mug/to_h.rb
24
26
  - lib/mug/fragile-method-chain.rb
25
27
  - lib/mug/self.rb
26
28
  - lib/mug/iterator.rb
@@ -31,10 +33,12 @@ files:
31
33
  - lib/mug/bool.rb
32
34
  - test/test-self.rb
33
35
  - test/test-bool.rb
36
+ - test/test-to_h.rb
34
37
  - test/test-fragile-method-chain.rb
35
38
  - test/test-iterator-method.rb
36
39
  - test/test-iterator-for.rb
37
40
  - test/test-maybe.rb
41
+ - test/test-hashmap.rb
38
42
  homepage: http://phluid61.github.com/mug
39
43
  licenses:
40
44
  - ISC License
@@ -62,7 +66,9 @@ summary: 'MUG: Matty''s Ultimate Gem'
62
66
  test_files:
63
67
  - test/test-self.rb
64
68
  - test/test-bool.rb
69
+ - test/test-to_h.rb
65
70
  - test/test-fragile-method-chain.rb
66
71
  - test/test-iterator-method.rb
67
72
  - test/test-iterator-for.rb
68
73
  - test/test-maybe.rb
74
+ - test/test-hashmap.rb