hashmap 0.2.0 → 1.0.0

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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/hashmap.rb +73 -55
  3. data/test/test_hashmap.rb +87 -0
  4. metadata +12 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b2582690c253d61d1d0157647883cb1e7e019afd
4
- data.tar.gz: 0aef9b23921cfcdd90dba1a1aebe5d8efd383f72
2
+ SHA256:
3
+ metadata.gz: 5e24bc493261f486e7bac722bdec9fc60711244204d003f715b3137538f5d796
4
+ data.tar.gz: 5d00b1b94b9340effb6f9f6edf1e040d22bf80fde4c9145d1ae0c7f99e6572d9
5
5
  SHA512:
6
- metadata.gz: e6d6f8ac18d9a9189868c67f44bc8d045e973668f511178946769e359004eda17dd9e2949656b67dd1f4a91348a9202f8ec078e84d67465508e53c1c7d19b9ec
7
- data.tar.gz: 8e22f67962881997485d48850f929f7a1f8b4e3192b3b0441434182287c79e6ea1dec2417ece098f55c7148f7efca6f70669d0c75bf56ebf343fd3db3e294ed5
6
+ metadata.gz: c5fe0ff6ec847586a69d7dd273813b713f050d021227f637688c6d8f5fb71a12a2e7eb326080f81925e449b2725b660e0afe6c3cbde6f7985335b2d15d54d2b3
7
+ data.tar.gz: 402c20315a1c01ade5a6e58d7223507debd8700a4146c9075e7ea71a312f4dc7e303d231a6b9b004937f52d47f427cb43d708a8fbfe9f8aaca91a6356b8ab4ba
@@ -1,66 +1,84 @@
1
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
- # If no block is given, an Enumerator is returned instead.
11
- #
12
- def map_values &block # :yields: value
13
- return enum_for(:map_values) unless block_given?
14
- hsh = {}
15
- each do |k, v|
16
- hsh[k] = yield v
17
- end
18
- hsh
2
+ if RUBY_VERSION >= '2.4'
3
+ class Hash
4
+ alias map_values transform_values
5
+ alias map_values! transform_values!
19
6
  end
7
+ else
8
+ class Hash
9
+ #
10
+ # Returns a new hash which is a copy of the current hash but each
11
+ # value is replaced by the result of running it through +block+.
12
+ #
13
+ # {'a'=>1, 'b'=>2}.map_values { |v| v*2 } #=> {'a'=>2, 'b'=>4}
14
+ # {'a'=>1, 'b'=>2}.map_values { "cat" } #=> {'a'=>"cat", 'b'=>"cat"}
15
+ #
16
+ # If no block is given, an Enumerator is returned instead.
17
+ #
18
+ def map_values &block # :yields: value
19
+ return enum_for(:map_values) unless block_given?
20
+ hsh = {}
21
+ each do |k, v|
22
+ hsh[k] = yield v
23
+ end
24
+ hsh
25
+ end
20
26
 
21
- #
22
- # Replaces the values in +hsh+ by running them each through +block+.
23
- #
24
- # See: #map_values
25
- #
26
- def map_values! &block # :yields: value
27
- return enum_for(:map_values!) unless block_given?
28
- replace map_values(&block)
27
+ #
28
+ # Replaces the values in +hsh+ by running them each through +block+.
29
+ #
30
+ # See: #map_values
31
+ #
32
+ def map_values! &block # :yields: value
33
+ return enum_for(:map_values!) unless block_given?
34
+ replace map_values(&block)
35
+ end
29
36
  end
37
+ end
30
38
 
31
- #
32
- # Returns a new hash which is a copy of the current hash but each
33
- # key is replaced by the result of running it through +block+.
34
- #
35
- # If +block+ returns duplicate keys, they will be overwritten in
36
- # the resulting hash.
37
- #
38
- # {'a'=>1, 'b'=>2}.map_keys { |k| k*2 } #=> {'aa'=>1, 'bb'=>2}
39
- # {'a'=>1, 'b'=>2}.map_keys { "cat" } #=> {'cat'=>2}
40
- #
41
- # If no block is given, an Enumerator is returned instead.
42
- #
43
- def map_keys &block # :yields: key
44
- return enum_for(:map_keys) unless block_given?
45
- hsh = {}
46
- each do |k, v|
47
- hsh[ yield k ] = v
48
- end
49
- hsh
39
+ if RUBY_VERSION >= '2.5'
40
+ class Hash
41
+ alias map_keys transform_keys
42
+ alias map_keys! transform_keys!
50
43
  end
44
+ else
45
+ class Hash
46
+ #
47
+ # Returns a new hash which is a copy of the current hash but each
48
+ # key is replaced by the result of running it through +block+.
49
+ #
50
+ # If +block+ returns duplicate keys, they will be overwritten in
51
+ # the resulting hash.
52
+ #
53
+ # {'a'=>1, 'b'=>2}.map_keys { |k| k*2 } #=> {'aa'=>1, 'bb'=>2}
54
+ # {'a'=>1, 'b'=>2}.map_keys { "cat" } #=> {'cat'=>2}
55
+ #
56
+ # If no block is given, an Enumerator is returned instead.
57
+ #
58
+ def map_keys &block # :yields: key
59
+ return enum_for(:map_keys) unless block_given?
60
+ hsh = {}
61
+ each do |k, v|
62
+ hsh[ yield k ] = v
63
+ end
64
+ hsh
65
+ end
51
66
 
52
- #
53
- # Replaces the keys in +hsh+ by running them each through +block+.
54
- #
55
- # If +block+ returns duplicate keys, they will be overwritten in turn.
56
- #
57
- # See: #map_keys
58
- #
59
- def map_keys! &block # :yields: key
60
- return enum_for(:map_keys!) unless block_given?
61
- replace map_keys(&block)
67
+ #
68
+ # Replaces the keys in +hsh+ by running them each through +block+.
69
+ #
70
+ # If +block+ returns duplicate keys, they will be overwritten in turn.
71
+ #
72
+ # See: #map_keys
73
+ #
74
+ def map_keys! &block # :yields: key
75
+ return enum_for(:map_keys!) unless block_given?
76
+ replace map_keys(&block)
77
+ end
62
78
  end
79
+ end
63
80
 
81
+ class Hash
64
82
  #
65
83
  # Returns a new hash which is a copy of the current hash but each
66
84
  # key-value pair is replaced by the result of running it through +block+.
@@ -95,7 +113,7 @@ class Hash
95
113
  end
96
114
 
97
115
  =begin
98
- Copyright (c) 2013-2016, Matthew Kerwin <matthew@kerwin.net.au>
116
+ Copyright (c) 2019, Matthew Kerwin <matthew@kerwin.net.au>
99
117
 
100
118
  Permission to use, copy, modify, and/or distribute this software for any
101
119
  purpose with or without fee is hereby granted, provided that the above
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+
3
+ if RUBY_VERSION.to_f < 1.9
4
+ class Symbol
5
+ def next
6
+ to_s.next.to_sym
7
+ end
8
+ end
9
+ end
10
+
11
+ $VERBOSE = true
12
+ require "#{File.dirname File.dirname(__FILE__)}/lib/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
+
24
+ def test_hashmap_bang
25
+ h = {'s'=>1, :x=>2, 3=>'a'}
26
+ h_k_next = {'t'=>1, :y=>2, 4=>'a'}
27
+ h_v_next = {'s'=>2, :x=>3, 3=>'b'}
28
+ h_p_next = {'t'=>2, :y=>3, 4=>'b'}
29
+
30
+ a = h.dup
31
+ a.map_keys! {|k| k.next }
32
+ assert_equal( h_k_next, a )
33
+
34
+ b = h.dup
35
+ b.map_values!{|v| v.next }
36
+ assert_equal( h_v_next, b )
37
+
38
+ c = h.dup
39
+ c.map_pairs!{|k,v| [k.next, v.next] }
40
+ assert_equal( h_p_next, c )
41
+ end
42
+
43
+ def test_hashmap_noblock
44
+ h = {'s'=>1, :x=>2, 3=>'a'}
45
+ h_k_next = {'t'=>1, :y=>2, 4=>'a'}
46
+ h_v_next = {'s'=>2, :x=>3, 3=>'b'}
47
+ h_p_next = {'t'=>2, :y=>3, 4=>'b'}
48
+
49
+ e = h.map_keys
50
+ assert_instance_of( Enumerator, e )
51
+ assert_equal( h_k_next, e.each {|k| k.next } )
52
+
53
+ e = h.map_values
54
+ assert_instance_of( Enumerator, e )
55
+ assert_equal( h_v_next, e.each {|v| v.next } )
56
+
57
+ e = h.map_pairs
58
+ assert_instance_of( Enumerator, e )
59
+ assert_equal( h_p_next, e.each {|k,v| [k.next,v.next] } )
60
+ end
61
+
62
+ def test_hashmap_bang_noblock
63
+ h = {'s'=>1, :x=>2, 3=>'a'}
64
+ h_k_next = {'t'=>1, :y=>2, 4=>'a'}
65
+ h_v_next = {'s'=>2, :x=>3, 3=>'b'}
66
+ h_p_next = {'t'=>2, :y=>3, 4=>'b'}
67
+
68
+ a = h.dup
69
+ e = a.map_keys!
70
+ assert_instance_of( Enumerator, e )
71
+ e.each {|k| k.next }
72
+ assert_equal( h_k_next, a )
73
+
74
+ b = h.dup
75
+ e = b.map_values!
76
+ assert_instance_of( Enumerator, e )
77
+ e.each {|v| v.next }
78
+ assert_equal( h_v_next, b )
79
+
80
+ c = h.dup
81
+ e = c.map_pairs!
82
+ assert_instance_of( Enumerator, e )
83
+ e.each {|k,v| [k.next, v.next ] }
84
+ assert_equal( h_p_next, c )
85
+ end
86
+ end
87
+
metadata CHANGED
@@ -1,23 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-15 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Defines some new mapping methods for Hash. See https://bugs.ruby-lang.org/issues/7793
13
+ description: |
14
+ Defines some new mapping methods for Hash.
15
+
16
+ See: https://bugs.ruby-lang.org/issues/7793
14
17
  email: matthew@kerwin.net.au
15
18
  executables: []
16
19
  extensions: []
17
20
  extra_rdoc_files: []
18
21
  files:
19
22
  - lib/hashmap.rb
20
- homepage: http://rubygems.org/gems/hashmap
23
+ - test/test_hashmap.rb
24
+ homepage: https://rubygems.org/gems/hashmap
21
25
  licenses:
22
26
  - ISC
23
27
  metadata: {}
@@ -36,9 +40,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
40
  - !ruby/object:Gem::Version
37
41
  version: '0'
38
42
  requirements: []
39
- rubyforge_project:
40
- rubygems_version: 2.5.1
43
+ rubygems_version: 3.0.1
41
44
  signing_key:
42
45
  specification_version: 4
43
- summary: New mappings for Hash
44
- test_files: []
46
+ summary: New mapping methods for Hash
47
+ test_files:
48
+ - test/test_hashmap.rb