hashmap 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/lib/hashmap.rb +104 -75
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2582690c253d61d1d0157647883cb1e7e019afd
|
4
|
+
data.tar.gz: 0aef9b23921cfcdd90dba1a1aebe5d8efd383f72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6d6f8ac18d9a9189868c67f44bc8d045e973668f511178946769e359004eda17dd9e2949656b67dd1f4a91348a9202f8ec078e84d67465508e53c1c7d19b9ec
|
7
|
+
data.tar.gz: 8e22f67962881997485d48850f929f7a1f8b4e3192b3b0441434182287c79e6ea1dec2417ece098f55c7148f7efca6f70669d0c75bf56ebf343fd3db3e294ed5
|
data/lib/hashmap.rb
CHANGED
@@ -1,83 +1,112 @@
|
|
1
|
+
|
1
2
|
class Hash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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)
|
29
|
+
end
|
25
30
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
50
|
+
end
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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)
|
62
|
+
end
|
54
63
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
64
|
+
#
|
65
|
+
# Returns a new hash which is a copy of the current hash but each
|
66
|
+
# key-value pair is replaced by the result of running it through +block+.
|
67
|
+
#
|
68
|
+
# If +block+ returns duplicate keys, they will be overwritten in
|
69
|
+
# the resulting hash.
|
70
|
+
#
|
71
|
+
# {'a'=>1, 'b'=>2}.map_pairs { |k,v| [k*2, v+1] } #=> {'aa'=>2, 'bb'=>3}
|
72
|
+
# {'a'=>1, 'b'=>2}.map_pairs { ["cat","dog"] } #=> {'cat'=>'dog'}
|
73
|
+
#
|
74
|
+
# If no block is given, an Enumerator is returned instead.
|
75
|
+
#
|
76
|
+
def map_pairs &block # :yields: key, value
|
77
|
+
return enum_for(:map_pairs) unless block_given?
|
78
|
+
hsh = {}
|
79
|
+
each do |k, v|
|
80
|
+
a, b = yield k, v
|
81
|
+
hsh[a] = b
|
82
|
+
end
|
83
|
+
hsh
|
84
|
+
end
|
73
85
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
86
|
+
#
|
87
|
+
# Replaces the values in +hsh+ by running them each through +block+.
|
88
|
+
#
|
89
|
+
# See: #map_pairs
|
90
|
+
#
|
91
|
+
def map_pairs! &block # :yields: key, value
|
92
|
+
return enum_for(:map_pairs!) unless block_given?
|
93
|
+
replace map_pairs(&block)
|
94
|
+
end
|
82
95
|
end
|
83
96
|
|
97
|
+
=begin
|
98
|
+
Copyright (c) 2013-2016, Matthew Kerwin <matthew@kerwin.net.au>
|
99
|
+
|
100
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
101
|
+
purpose with or without fee is hereby granted, provided that the above
|
102
|
+
copyright notice and this permission notice appear in all copies.
|
103
|
+
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
105
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
106
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
107
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
108
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
109
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
110
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
111
|
+
=end
|
112
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashmap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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:
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Defines some new mapping methods for Hash. See https://bugs.ruby-lang.org/issues/7793
|
14
14
|
email: matthew@kerwin.net.au
|
@@ -19,7 +19,7 @@ files:
|
|
19
19
|
- lib/hashmap.rb
|
20
20
|
homepage: http://rubygems.org/gems/hashmap
|
21
21
|
licenses:
|
22
|
-
-
|
22
|
+
- ISC
|
23
23
|
metadata: {}
|
24
24
|
post_install_message:
|
25
25
|
rdoc_options: []
|
@@ -27,17 +27,17 @@ require_paths:
|
|
27
27
|
- lib
|
28
28
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0'
|
33
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ">="
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
39
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
40
|
+
rubygems_version: 2.5.1
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
43
|
summary: New mappings for Hash
|