hashmap 0.1.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 +7 -0
- data/lib/hashmap.rb +83 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c20f904760916de0dcc2353dbee373135432d9ac
|
4
|
+
data.tar.gz: e4eeb6d7851f489967b7da00ab71adf2c2c004ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08089466608a69e5770cb5b06ded0cda5501cf85dc3b8f160504d378ccc0510b4cfe868fc2b01a657eac9ab2d575c523e03bd3625bff0a181f572a8061b02c8a
|
7
|
+
data.tar.gz: 1ec74d7f3ea5319cd56a035b63ef4140817fc4e6c8250f732b7295dc6dedb16dd67b9726d5a2ff6f9ccbd574a72e245b15c6d7f70780d78f9cb26c7ab45c148a
|
data/lib/hashmap.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
class Hash
|
2
|
+
#
|
3
|
+
# Returns a new hash which is a copy of the current hash but each
|
4
|
+
# value is replaced by the result of running it through +block+.
|
5
|
+
#
|
6
|
+
# {'a'=>1, 'b'=>2}.map_values { |v| v*2 } #=> {'a'=>2, 'b'=>4}
|
7
|
+
# {'a'=>1, 'b'=>2}.map_values { "cat" } #=> {'a'=>"cat", 'b'=>"cat"}
|
8
|
+
#
|
9
|
+
def map_values &block # :yields: value
|
10
|
+
hsh = {}
|
11
|
+
each do |k, v|
|
12
|
+
hsh[k] = yield v
|
13
|
+
end
|
14
|
+
hsh
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Replaces the values in +hsh+ by running them each through +block+.
|
19
|
+
#
|
20
|
+
# See: #map_values
|
21
|
+
#
|
22
|
+
def map_values! &block # :yields: value
|
23
|
+
replace map_values(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Returns a new hash which is a copy of the current hash but each
|
28
|
+
# key is replaced by the result of running it through +block+.
|
29
|
+
#
|
30
|
+
# If +block+ returns duplicate keys, they will be overwritten in
|
31
|
+
# the resulting hash.
|
32
|
+
#
|
33
|
+
# {'a'=>1, 'b'=>2}.map_keys { |k| k*2 } #=> {'aa'=>1, 'bb'=>2}
|
34
|
+
# {'a'=>1, 'b'=>2}.map_keys { "cat" } #=> {'cat'=>2}
|
35
|
+
#
|
36
|
+
def map_keys &block # :yields: key
|
37
|
+
hsh = {}
|
38
|
+
each do |k, v|
|
39
|
+
hsh[ yield k ] = v
|
40
|
+
end
|
41
|
+
hsh
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Replaces the keys in +hsh+ by running them each through +block+.
|
46
|
+
#
|
47
|
+
# If +block+ returns duplicate keys, they will be overwritten in turn.
|
48
|
+
#
|
49
|
+
# See: #map_keys
|
50
|
+
#
|
51
|
+
def map_keys! &block # :yields: key
|
52
|
+
replace map_keys(&block)
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Returns a new hash which is a copy of the current hash but each
|
57
|
+
# key-value pair is replaced by the result of running it through +block+.
|
58
|
+
#
|
59
|
+
# If +block+ returns duplicate keys, they will be overwritten in
|
60
|
+
# the resulting hash.
|
61
|
+
#
|
62
|
+
# {'a'=>1, 'b'=>2}.map_pairs { |k,v| [k*2, v+1] } #=> {'aa'=>2, 'bb'=>3}
|
63
|
+
# {'a'=>1, 'b'=>2}.map_pairs { ["cat","dog"] } #=> {'cat'=>'dog'}
|
64
|
+
#
|
65
|
+
def map_pairs &block # :yields: key, value
|
66
|
+
hsh = {}
|
67
|
+
each do |k, v|
|
68
|
+
a, b = yield k, v
|
69
|
+
hsh[a] = b
|
70
|
+
end
|
71
|
+
hsh
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Replaces the values in +hsh+ by running them each through +block+.
|
76
|
+
#
|
77
|
+
# See: #map_values
|
78
|
+
#
|
79
|
+
def map_pairs! &block # :yields: key, value
|
80
|
+
replace map_pairs(&block)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Kerwin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Defines some new mapping methods for Hash. See https://bugs.ruby-lang.org/issues/7793
|
14
|
+
email: matthew@kerwin.net.au
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/hashmap.rb
|
20
|
+
homepage: http://rubygems.org/gems/hashmap
|
21
|
+
licenses:
|
22
|
+
- Simplified BSD License
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.0
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: New mappings for Hash
|
44
|
+
test_files: []
|