hash-utils 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +8 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -5
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/Rakefile +3 -4
- data/VERSION +1 -1
- data/hash-utils.gemspec +8 -8
- data/lib/hash-utils/array.rb +120 -92
- data/lib/hash-utils/boolean.rb +43 -21
- data/lib/hash-utils/file.rb +61 -43
- data/lib/hash-utils/gem.rb +16 -12
- data/lib/hash-utils/hash.rb +306 -264
- data/lib/hash-utils/io.rb +7 -3
- data/lib/hash-utils/module.rb +14 -10
- data/lib/hash-utils/nil.rb +7 -3
- data/lib/hash-utils/numeric.rb +26 -12
- data/lib/hash-utils/object.rb +165 -52
- data/lib/hash-utils/proc.rb +7 -3
- data/lib/hash-utils/string.rb +289 -173
- data/lib/hash-utils/stringio.rb +7 -3
- data/lib/hash-utils/symbol.rb +47 -24
- data/test +7 -1
- metadata +20 -22
data/lib/hash-utils/boolean.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/object"
|
3
5
|
|
4
6
|
##
|
5
7
|
# +TrueClass+ extension.
|
@@ -15,8 +17,10 @@ class TrueClass
|
|
15
17
|
# @since 0.17.0
|
16
18
|
#
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
+
if not self.__hash_utils_instance_respond_to? :boolean?
|
21
|
+
def boolean?
|
22
|
+
true
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
##
|
@@ -26,8 +30,10 @@ class TrueClass
|
|
26
30
|
# @since 0.15.0
|
27
31
|
#
|
28
32
|
|
29
|
-
|
30
|
-
false
|
33
|
+
if not self.__hash_utils_instance_respond_to? :false?
|
34
|
+
def false?
|
35
|
+
false
|
36
|
+
end
|
31
37
|
end
|
32
38
|
|
33
39
|
##
|
@@ -37,8 +43,10 @@ class TrueClass
|
|
37
43
|
# @since 0.15.0
|
38
44
|
#
|
39
45
|
|
40
|
-
|
41
|
-
true
|
46
|
+
if not self.__hash_utils_instance_respond_to? :true?
|
47
|
+
def true?
|
48
|
+
true
|
49
|
+
end
|
42
50
|
end
|
43
51
|
|
44
52
|
##
|
@@ -50,8 +58,10 @@ class TrueClass
|
|
50
58
|
# @since 0.19.0
|
51
59
|
#
|
52
60
|
|
53
|
-
|
54
|
-
t
|
61
|
+
if not self.__hash_utils_instance_respond_to? :convert
|
62
|
+
def convert(t, f)
|
63
|
+
t
|
64
|
+
end
|
55
65
|
end
|
56
66
|
|
57
67
|
##
|
@@ -63,8 +73,10 @@ class TrueClass
|
|
63
73
|
# @since 0.19.0
|
64
74
|
#
|
65
75
|
|
66
|
-
|
67
|
-
|
76
|
+
if not self.__hash_utils_instance_respond_to? :to_i
|
77
|
+
def to_i(t = 1, f = 0)
|
78
|
+
self.convert(t, f)
|
79
|
+
end
|
68
80
|
end
|
69
81
|
|
70
82
|
end
|
@@ -83,8 +95,10 @@ class FalseClass
|
|
83
95
|
# @since 0.17.0
|
84
96
|
#
|
85
97
|
|
86
|
-
|
87
|
-
|
98
|
+
if not self.__hash_utils_instance_respond_to? :boolean?
|
99
|
+
def boolean?
|
100
|
+
true
|
101
|
+
end
|
88
102
|
end
|
89
103
|
|
90
104
|
##
|
@@ -94,8 +108,10 @@ class FalseClass
|
|
94
108
|
# @since 0.15.0
|
95
109
|
#
|
96
110
|
|
97
|
-
|
98
|
-
|
111
|
+
if not self.__hash_utils_instance_respond_to? :false?
|
112
|
+
def false?
|
113
|
+
true
|
114
|
+
end
|
99
115
|
end
|
100
116
|
|
101
117
|
##
|
@@ -105,8 +121,10 @@ class FalseClass
|
|
105
121
|
# @since 0.15.0
|
106
122
|
#
|
107
123
|
|
108
|
-
|
109
|
-
|
124
|
+
if not self.__hash_utils_instance_respond_to? :true?
|
125
|
+
def true?
|
126
|
+
false
|
127
|
+
end
|
110
128
|
end
|
111
129
|
|
112
130
|
##
|
@@ -118,8 +136,10 @@ class FalseClass
|
|
118
136
|
# @since 0.19.0
|
119
137
|
#
|
120
138
|
|
121
|
-
|
122
|
-
f
|
139
|
+
if not self.__hash_utils_instance_respond_to? :convert
|
140
|
+
def convert(t, f)
|
141
|
+
f
|
142
|
+
end
|
123
143
|
end
|
124
144
|
|
125
145
|
##
|
@@ -131,8 +151,10 @@ class FalseClass
|
|
131
151
|
# @since 0.19.0
|
132
152
|
#
|
133
153
|
|
134
|
-
|
135
|
-
|
154
|
+
if not self.__hash_utils_instance_respond_to? :to_i
|
155
|
+
def to_i(t = 1, f = 0)
|
156
|
+
self.convert(t, f)
|
157
|
+
end
|
136
158
|
end
|
137
159
|
|
138
160
|
end
|
data/lib/hash-utils/file.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
4
|
require "ruby-version"
|
5
|
+
require "hash-utils/object"
|
5
6
|
|
6
7
|
##
|
7
8
|
# File extension.
|
@@ -11,51 +12,57 @@ require "ruby-version"
|
|
11
12
|
class File
|
12
13
|
|
13
14
|
|
14
|
-
if
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
15
|
+
if not self.respond_to? :write
|
16
|
+
if Ruby::Version < [1, 9, 3]
|
17
|
+
|
18
|
+
##
|
19
|
+
# Writes data to file and closes it in single call.
|
20
|
+
#
|
21
|
+
# @note Data were written in binary mode since +0.11.1+,
|
22
|
+
# but since +0.19.0+, they are written non-binary again.
|
23
|
+
# Binary writing is implemented via {File#binwrite}.
|
24
|
+
# @note Since Ruby 1.9.3 replaced by STL native version.
|
25
|
+
#
|
26
|
+
# @param [String] filepath path to file
|
27
|
+
# @param [String] data data for write
|
28
|
+
# @return [Integer] length of really written data
|
29
|
+
# @since 0.11.0
|
30
|
+
#
|
31
|
+
|
32
|
+
def self.write(filepath, data = "")
|
33
|
+
len = nil
|
34
|
+
File.open(filepath, "w") do |io|
|
35
|
+
len = io.write(data)
|
36
|
+
end
|
37
|
+
return len
|
34
38
|
end
|
35
|
-
|
39
|
+
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
39
|
-
if
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
43
|
+
if not self.respond_to? :binwrite
|
44
|
+
if Ruby::Version < [1, 9, 3]
|
45
|
+
|
46
|
+
##
|
47
|
+
# Writes binary data to file and closes it
|
48
|
+
# in single call.
|
49
|
+
#
|
50
|
+
# @note Since Ruby 1.9.3 replaced by STL native version.
|
51
|
+
#
|
52
|
+
# @param [String] filepath path to file
|
53
|
+
# @param [String] data data for write
|
54
|
+
# @return [Integer] length of really written data
|
55
|
+
# @since 0.19.0
|
56
|
+
#
|
57
|
+
|
58
|
+
def self.binwrite(filepath, data = "")
|
59
|
+
len = nil
|
60
|
+
File.open(filepath, "wb") do |io|
|
61
|
+
len = io.write(data)
|
62
|
+
end
|
63
|
+
return len
|
57
64
|
end
|
58
|
-
|
65
|
+
|
59
66
|
end
|
60
67
|
end
|
61
68
|
|
@@ -67,8 +74,19 @@ class File
|
|
67
74
|
# @since 0.11.0
|
68
75
|
#
|
69
76
|
|
70
|
-
|
71
|
-
|
77
|
+
if not self.respond_to? :touch
|
78
|
+
def self.touch(filepath)
|
79
|
+
File.open(filepath, "wb").close()
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Alias for #close.
|
85
|
+
# @since 2.0.0
|
86
|
+
#
|
87
|
+
|
88
|
+
if not self.__hash_utils_instance_respond_to? :close!
|
89
|
+
alias :close! :close
|
72
90
|
end
|
73
91
|
|
74
92
|
end
|
data/lib/hash-utils/gem.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
4
|
require "hash-utils/object"
|
5
5
|
|
@@ -18,11 +18,13 @@ module Gem
|
|
18
18
|
# @since 0.17.1
|
19
19
|
#
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
if not self.respond_to? :available?
|
22
|
+
def self.available?(name)
|
23
|
+
begin
|
24
|
+
return Gem::Specification::find_by_name(name.to_s).to_b
|
25
|
+
rescue Gem::LoadError
|
26
|
+
return false
|
27
|
+
end
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
@@ -36,12 +38,14 @@ module Gem
|
|
36
38
|
# @since 0.17.1
|
37
39
|
#
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
if not self.respond_to? :require_available
|
42
|
+
def self.require_available(name, file = nil)
|
43
|
+
if self.available? name
|
44
|
+
require file.nil? ? name : file
|
45
|
+
true
|
46
|
+
else
|
47
|
+
false
|
48
|
+
end
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
data/lib/hash-utils/hash.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
+
require "hash-utils/object"
|
4
5
|
require "hash-utils/array"
|
5
6
|
|
6
7
|
##
|
@@ -20,9 +21,11 @@ class Hash
|
|
20
21
|
# @since 0.3.0
|
21
22
|
#
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
if not self.respond_to? :define
|
25
|
+
def self.define(values = { }, default = nil, &block)
|
26
|
+
hash = self[values]
|
27
|
+
self::create(default, hash, &block)
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
31
|
##
|
@@ -35,14 +38,12 @@ class Hash
|
|
35
38
|
# @since 0.3.0
|
36
39
|
#
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
hash
|
41
|
+
if not self.respond_to? :create
|
42
|
+
def self.create(default = nil, hash = { }, &block)
|
43
|
+
hash.default = default
|
44
|
+
hash.default_proc = block if not block.nil?
|
45
|
+
return hash
|
43
46
|
end
|
44
|
-
|
45
|
-
return hash
|
46
47
|
end
|
47
48
|
|
48
49
|
##
|
@@ -52,9 +53,11 @@ class Hash
|
|
52
53
|
# @return [Hash] new hash
|
53
54
|
# @since 0.3.0
|
54
55
|
#
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
|
57
|
+
if not self.__hash_utils_instance_respond_to? :recreate
|
58
|
+
def recreate
|
59
|
+
self.class::create(self.default, &self.default_proc)
|
60
|
+
end
|
58
61
|
end
|
59
62
|
|
60
63
|
##
|
@@ -65,8 +68,10 @@ class Hash
|
|
65
68
|
# @since 0.3.0
|
66
69
|
#
|
67
70
|
|
68
|
-
|
69
|
-
|
71
|
+
if not self.__hash_utils_instance_respond_to? :recreate!
|
72
|
+
def recreate!
|
73
|
+
self.replace(self.recreate)
|
74
|
+
end
|
70
75
|
end
|
71
76
|
|
72
77
|
##
|
@@ -78,47 +83,26 @@ class Hash
|
|
78
83
|
# @since 0.3.0
|
79
84
|
#
|
80
85
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
86
|
+
if not self.__hash_utils_instance_respond_to? :remove!
|
87
|
+
def remove!(&block)
|
88
|
+
result = self.recreate
|
89
|
+
delete = [ ]
|
90
|
+
|
91
|
+
self.each_pair do |k, v|
|
92
|
+
if block.call(k, v)
|
93
|
+
result[k] = v
|
94
|
+
delete << k
|
95
|
+
end
|
89
96
|
end
|
97
|
+
|
98
|
+
delete.each do |k|
|
99
|
+
self.delete(k)
|
100
|
+
end
|
101
|
+
|
102
|
+
return result
|
90
103
|
end
|
91
|
-
|
92
|
-
delete.each do |k|
|
93
|
-
self.delete(k)
|
94
|
-
end
|
95
|
-
|
96
|
-
return result
|
97
|
-
end
|
98
|
-
|
99
|
-
##
|
100
|
-
# Returns a copy of +self+ with all +nil+ elements removed.
|
101
|
-
#
|
102
|
-
# @return [Hash] new hash
|
103
|
-
# @since 0.1.0
|
104
|
-
#
|
105
|
-
|
106
|
-
def compact
|
107
|
-
self.clean()
|
108
|
-
end
|
109
|
-
|
110
|
-
##
|
111
|
-
# Removes +nil+ elements from the hash. Returns +nil+
|
112
|
-
# if no changes were made, otherwise returns +self+.
|
113
|
-
#
|
114
|
-
# @return [Hash] new hash
|
115
|
-
# @since 0.1.0
|
116
|
-
#
|
117
|
-
|
118
|
-
def compact!
|
119
|
-
self.clean!
|
120
104
|
end
|
121
|
-
|
105
|
+
|
122
106
|
##
|
123
107
|
# Removes given value from the hash. It's +nil+ by default,
|
124
108
|
# so behaves just as {#compact}.
|
@@ -144,7 +128,30 @@ class Hash
|
|
144
128
|
def clean!(value = nil)
|
145
129
|
self.reject! { |k, v| v === value }
|
146
130
|
end
|
147
|
-
|
131
|
+
|
132
|
+
##
|
133
|
+
# Returns a copy of +self+ with all +nil+ elements removed.
|
134
|
+
#
|
135
|
+
# @return [Hash] new hash
|
136
|
+
# @since 0.1.0
|
137
|
+
#
|
138
|
+
|
139
|
+
if not self.__hash_utils_instance_respond_to? :compact
|
140
|
+
alias :compact :clean
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# Removes +nil+ elements from the hash. Returns +nil+
|
145
|
+
# if no changes were made, otherwise returns +self+.
|
146
|
+
#
|
147
|
+
# @return [Hash] new hash
|
148
|
+
# @since 0.1.0
|
149
|
+
#
|
150
|
+
|
151
|
+
if not self.__hash_utils_instance_respond_to? :compact!
|
152
|
+
alias :compact! :clean!
|
153
|
+
end
|
154
|
+
|
148
155
|
##
|
149
156
|
# Returns a new hash with the results of running block once for
|
150
157
|
# every pair in +self+.
|
@@ -154,18 +161,22 @@ class Hash
|
|
154
161
|
# @since 0.1.0
|
155
162
|
#
|
156
163
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
164
|
+
if not self.__hash_utils_instance_respond_to? :map_pairs
|
165
|
+
def map_pairs(&block)
|
166
|
+
_new = self.recreate
|
167
|
+
|
168
|
+
self.each_pair do |k, v|
|
169
|
+
new_k, new_v = block.call(k, v)
|
170
|
+
_new[new_k] = new_v
|
171
|
+
end
|
172
|
+
|
173
|
+
return _new
|
163
174
|
end
|
164
|
-
|
165
|
-
return new
|
166
175
|
end
|
167
176
|
|
168
|
-
|
177
|
+
if not self.__hash_utils_instance_respond_to? :collect_pairs
|
178
|
+
alias :collect_pairs :map_pairs
|
179
|
+
end
|
169
180
|
|
170
181
|
##
|
171
182
|
# Emulates {#map_pairs} on place. In fact, replaces old hash by
|
@@ -176,11 +187,15 @@ class Hash
|
|
176
187
|
# @since 0.1.0
|
177
188
|
#
|
178
189
|
|
179
|
-
|
180
|
-
|
190
|
+
if not self.__hash_utils_instance_respond_to? :map_pairs!
|
191
|
+
def map_pairs!(&block)
|
192
|
+
self.replace(self.map_pairs(&block))
|
193
|
+
end
|
181
194
|
end
|
182
195
|
|
183
|
-
|
196
|
+
if not self.__hash_utils_instance_respond_to? :collect_pairs!
|
197
|
+
alias :collect_pairs! :map_pairs!
|
198
|
+
end
|
184
199
|
|
185
200
|
##
|
186
201
|
# Returns a new hash with the results of running block once for
|
@@ -191,13 +206,17 @@ class Hash
|
|
191
206
|
# @since 0.1.0
|
192
207
|
#
|
193
208
|
|
194
|
-
|
195
|
-
|
196
|
-
|
209
|
+
if not self.__hash_utils_instance_respond_to? :map_keys
|
210
|
+
def map_keys(&block)
|
211
|
+
self.map_pairs do |k, v|
|
212
|
+
[block.call(k), v]
|
213
|
+
end
|
197
214
|
end
|
198
215
|
end
|
199
216
|
|
200
|
-
|
217
|
+
if not self.__hash_utils_instance_respond_to? :collect_keys
|
218
|
+
alias :collect_keys :map_keys
|
219
|
+
end
|
201
220
|
|
202
221
|
##
|
203
222
|
# Emulates {#map_keys} on place. In fact, replaces old hash by
|
@@ -208,11 +227,15 @@ class Hash
|
|
208
227
|
# @since 0.1.0
|
209
228
|
#
|
210
229
|
|
211
|
-
|
212
|
-
|
230
|
+
if not self.__hash_utils_instance_respond_to? :map_keys!
|
231
|
+
def map_keys!(&block)
|
232
|
+
self.replace(self.map_keys(&block))
|
233
|
+
end
|
213
234
|
end
|
214
235
|
|
215
|
-
|
236
|
+
if not self.__hash_utils_instance_respond_to? :collect_keys!
|
237
|
+
alias :collect_keys! :map_keys!
|
238
|
+
end
|
216
239
|
|
217
240
|
##
|
218
241
|
# Returns a new hash with the results of running block once for
|
@@ -223,13 +246,17 @@ class Hash
|
|
223
246
|
# @since 0.11.0
|
224
247
|
#
|
225
248
|
|
226
|
-
|
227
|
-
|
228
|
-
|
249
|
+
if not self.__hash_utils_instance_respond_to? :map_values
|
250
|
+
def map_values(&block)
|
251
|
+
self.map_pairs do |k, v|
|
252
|
+
[k, block.call(v)]
|
253
|
+
end
|
229
254
|
end
|
230
255
|
end
|
231
256
|
|
232
|
-
|
257
|
+
if not self.__hash_utils_instance_respond_to? :collect_values
|
258
|
+
alias :collect_values :map_values
|
259
|
+
end
|
233
260
|
|
234
261
|
##
|
235
262
|
# Emulates {#map_values} on place. In fact, replaces old hash by
|
@@ -240,11 +267,15 @@ class Hash
|
|
240
267
|
# @since 0.11.0
|
241
268
|
#
|
242
269
|
|
243
|
-
|
244
|
-
|
270
|
+
if not self.__hash_utils_instance_respond_to? :map_values!
|
271
|
+
def map_values!(&block)
|
272
|
+
self.replace(self.map_values(&block))
|
273
|
+
end
|
245
274
|
end
|
246
275
|
|
247
|
-
|
276
|
+
if not self.__hash_utils_instance_respond_to? :collect_values!
|
277
|
+
alias :collect_values! :map_values!
|
278
|
+
end
|
248
279
|
|
249
280
|
##
|
250
281
|
# Converts all keys to symbols. Since version +0.14.1+ converts
|
@@ -254,17 +285,21 @@ class Hash
|
|
254
285
|
# @since 0.1.0
|
255
286
|
#
|
256
287
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
k.
|
261
|
-
|
262
|
-
|
288
|
+
if not self.__hash_utils_instance_respond_to? :keys_to_sym
|
289
|
+
def keys_to_sym
|
290
|
+
self.map_keys do |k|
|
291
|
+
if k.kind_of? String
|
292
|
+
k.to_sym
|
293
|
+
else
|
294
|
+
k
|
295
|
+
end
|
263
296
|
end
|
264
297
|
end
|
265
298
|
end
|
266
299
|
|
267
|
-
|
300
|
+
if not self.__hash_utils_instance_respond_to? :symbolize_keys
|
301
|
+
alias :symbolize_keys :keys_to_sym
|
302
|
+
end
|
268
303
|
|
269
304
|
##
|
270
305
|
# Emulates {#keys_to_sym} on place. In fact, replaces old hash by
|
@@ -274,48 +309,15 @@ class Hash
|
|
274
309
|
# @since 0.1.0
|
275
310
|
#
|
276
311
|
|
277
|
-
|
278
|
-
|
312
|
+
if not self.__hash_utils_instance_respond_to? :keys_to_sym!
|
313
|
+
def keys_to_sym!
|
314
|
+
self.replace(self.keys_to_sym)
|
315
|
+
end
|
279
316
|
end
|
280
317
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
# Checks, all elements values follow condition expressed in block.
|
285
|
-
# Block must return boolean.
|
286
|
-
#
|
287
|
-
# If it's empty, returns +true+.
|
288
|
-
#
|
289
|
-
# @param [Proc] block checking block
|
290
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
291
|
-
# @note This method is currently in conflict with Ruby 1.9.2
|
292
|
-
# +Hash#all?+ method. Given block arity is checked, so code should
|
293
|
-
# be compatible for now, but this method will be probably moved
|
294
|
-
# around +0.13.0+ for performance reasons of sure to deprecated
|
295
|
-
# module.
|
296
|
-
# @deprecated (since 0.10.0, conflict with built-in method)
|
297
|
-
# @since 0.2.0
|
298
|
-
#
|
299
|
-
#
|
300
|
-
# REMOVED SINCE 0.15.0
|
301
|
-
#
|
302
|
-
# def all?(&block)
|
303
|
-
# if block.arity == 2
|
304
|
-
# self.all_pairs? &block
|
305
|
-
# end
|
306
|
-
#
|
307
|
-
# if self.empty? or block.nil?
|
308
|
-
# return true
|
309
|
-
# end
|
310
|
-
#
|
311
|
-
# self.each_value do |v|
|
312
|
-
# if block.call(v) == false
|
313
|
-
# return false
|
314
|
-
# end
|
315
|
-
# end
|
316
|
-
#
|
317
|
-
# return true
|
318
|
-
# end
|
318
|
+
if not self.__hash_utils_instance_respond_to? :symbolize_keys!
|
319
|
+
alias :symbolize_keys! :keys_to_sym!
|
320
|
+
end
|
319
321
|
|
320
322
|
##
|
321
323
|
# Checks, all elements follow condition expressed in block.
|
@@ -329,19 +331,19 @@ class Hash
|
|
329
331
|
# Ruby built-in +#all?+.
|
330
332
|
# @since 0.2.0
|
331
333
|
#
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
return true
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
334
|
+
|
335
|
+
if not self.__hash_utils_instance_respond_to? :all_pairs?
|
336
|
+
def all_pairs?(&block)
|
337
|
+
return true if self.empty?
|
338
|
+
|
339
|
+
self.each_pair do |k, v|
|
340
|
+
if block.call(k, v) == false
|
341
|
+
return false
|
342
|
+
end
|
341
343
|
end
|
344
|
+
|
345
|
+
return true
|
342
346
|
end
|
343
|
-
|
344
|
-
return true
|
345
347
|
end
|
346
348
|
|
347
349
|
##
|
@@ -352,10 +354,12 @@ class Hash
|
|
352
354
|
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
353
355
|
# @since 0.2.0
|
354
356
|
#
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
357
|
+
|
358
|
+
if not self.__hash_utils_instance_respond_to? :some?
|
359
|
+
def some?(&block)
|
360
|
+
self.one? do |pair|
|
361
|
+
block.call(pair[1])
|
362
|
+
end
|
359
363
|
end
|
360
364
|
end
|
361
365
|
|
@@ -368,7 +372,9 @@ class Hash
|
|
368
372
|
# @since 0.2.0
|
369
373
|
#
|
370
374
|
|
371
|
-
|
375
|
+
if not self.__hash_utils_instance_respond_to? :some_pairs?
|
376
|
+
alias :some_pairs? :one?
|
377
|
+
end
|
372
378
|
|
373
379
|
##
|
374
380
|
# Compatibility method with {Array#to_h}. Returns itself.
|
@@ -377,8 +383,10 @@ class Hash
|
|
377
383
|
# @since 0.4.0
|
378
384
|
#
|
379
385
|
|
380
|
-
|
381
|
-
|
386
|
+
if not self.__hash_utils_instance_respond_to? :to_h
|
387
|
+
def to_h(mode = nil)
|
388
|
+
self
|
389
|
+
end
|
382
390
|
end
|
383
391
|
|
384
392
|
##
|
@@ -389,8 +397,10 @@ class Hash
|
|
389
397
|
# @since 0.5.0
|
390
398
|
#
|
391
399
|
|
392
|
-
|
393
|
-
|
400
|
+
if not self.__hash_utils_instance_respond_to? :sort!
|
401
|
+
def sort!(&block)
|
402
|
+
self.replace(Hash[self.sort(&block)])
|
403
|
+
end
|
394
404
|
end
|
395
405
|
|
396
406
|
##
|
@@ -406,11 +416,13 @@ class Hash
|
|
406
416
|
# @since 0.5.0
|
407
417
|
#
|
408
418
|
|
409
|
-
|
410
|
-
|
411
|
-
|
419
|
+
if not self.__hash_utils_instance_respond_to? :ksort
|
420
|
+
def ksort(&block)
|
421
|
+
if block.nil?
|
422
|
+
block = Proc::new { |a, b| a <=> b }
|
423
|
+
end
|
424
|
+
Hash[self.sort { |a, b| block.call(a[0], b[0]) }]
|
412
425
|
end
|
413
|
-
Hash[self.sort { |a, b| block.call(a[0], b[0]) }]
|
414
426
|
end
|
415
427
|
|
416
428
|
##
|
@@ -422,10 +434,12 @@ class Hash
|
|
422
434
|
# @since 0.5.0
|
423
435
|
#
|
424
436
|
|
425
|
-
|
426
|
-
|
437
|
+
if not self.__hash_utils_instance_respond_to? :ksort!
|
438
|
+
def ksort!(&block)
|
439
|
+
self.replace(self.ksort(&block))
|
440
|
+
end
|
427
441
|
end
|
428
|
-
|
442
|
+
|
429
443
|
##
|
430
444
|
# Sorts hash according to values. Keeps key associations.
|
431
445
|
# It's equivalent of PHP asort().
|
@@ -440,11 +454,13 @@ class Hash
|
|
440
454
|
# @since 0.5.0
|
441
455
|
#
|
442
456
|
|
443
|
-
|
444
|
-
|
445
|
-
|
457
|
+
if not self.__hash_utils_instance_respond_to? :asort
|
458
|
+
def asort(&block)
|
459
|
+
if block.nil?
|
460
|
+
block = Proc::new { |a, b| a <=> b }
|
461
|
+
end
|
462
|
+
Hash[self.sort { |a, b| block.call(a[1], b[1]) }]
|
446
463
|
end
|
447
|
-
Hash[self.sort { |a, b| block.call(a[1], b[1]) }]
|
448
464
|
end
|
449
465
|
|
450
466
|
##
|
@@ -456,8 +472,10 @@ class Hash
|
|
456
472
|
# @since 0.5.0
|
457
473
|
#
|
458
474
|
|
459
|
-
|
460
|
-
|
475
|
+
if not self.__hash_utils_instance_respond_to? :asort!
|
476
|
+
def asort!(&block)
|
477
|
+
self.replace(self.asort(&block))
|
478
|
+
end
|
461
479
|
end
|
462
480
|
|
463
481
|
##
|
@@ -470,8 +488,10 @@ class Hash
|
|
470
488
|
# @since 0.5.0
|
471
489
|
#
|
472
490
|
|
473
|
-
|
474
|
-
|
491
|
+
if not self.__hash_utils_instance_respond_to? :reverse
|
492
|
+
def reverse
|
493
|
+
Hash[self.to_a.reverse]
|
494
|
+
end
|
475
495
|
end
|
476
496
|
|
477
497
|
##
|
@@ -481,8 +501,10 @@ class Hash
|
|
481
501
|
# @since 0.5.0
|
482
502
|
#
|
483
503
|
|
484
|
-
|
485
|
-
|
504
|
+
if not self.__hash_utils_instance_respond_to? :reverse!
|
505
|
+
def reverse!
|
506
|
+
self.replace(self.reverse)
|
507
|
+
end
|
486
508
|
end
|
487
509
|
|
488
510
|
##
|
@@ -493,17 +515,21 @@ class Hash
|
|
493
515
|
# @since 0.9.0
|
494
516
|
#
|
495
517
|
|
496
|
-
|
497
|
-
keys
|
498
|
-
|
499
|
-
|
518
|
+
if not self.__hash_utils_instance_respond_to? :has_all?
|
519
|
+
def has_all?(keys)
|
520
|
+
keys.each do |key|
|
521
|
+
if not self.has_key? key
|
522
|
+
return false
|
523
|
+
end
|
500
524
|
end
|
525
|
+
|
526
|
+
return true
|
501
527
|
end
|
502
|
-
|
503
|
-
return true
|
504
528
|
end
|
505
529
|
|
506
|
-
|
530
|
+
if not self.__hash_utils_instance_respond_to? :has_keys?
|
531
|
+
alias :has_keys? :has_all?
|
532
|
+
end
|
507
533
|
|
508
534
|
##
|
509
535
|
# Indicates, some of the keys are available in Hash.
|
@@ -513,14 +539,16 @@ class Hash
|
|
513
539
|
# @since 0.9.0
|
514
540
|
#
|
515
541
|
|
516
|
-
|
517
|
-
keys
|
518
|
-
|
519
|
-
|
542
|
+
if not self.__hash_utils_instance_respond_to? :has_some?
|
543
|
+
def has_some?(keys)
|
544
|
+
keys.each do |key|
|
545
|
+
if self.has_key? key
|
546
|
+
return true
|
547
|
+
end
|
520
548
|
end
|
549
|
+
|
550
|
+
return false
|
521
551
|
end
|
522
|
-
|
523
|
-
return false
|
524
552
|
end
|
525
553
|
|
526
554
|
##
|
@@ -539,13 +567,15 @@ class Hash
|
|
539
567
|
# @since 0.10.0
|
540
568
|
#
|
541
569
|
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
570
|
+
if not self.respond_to? :combine
|
571
|
+
def self.combine(keys, values)
|
572
|
+
result = { }
|
573
|
+
keys.each_index do |i|
|
574
|
+
result[keys[i]] = values[i]
|
575
|
+
end
|
576
|
+
|
577
|
+
return result
|
546
578
|
end
|
547
|
-
|
548
|
-
return result
|
549
579
|
end
|
550
580
|
|
551
581
|
##
|
@@ -557,21 +587,23 @@ class Hash
|
|
557
587
|
# @since 0.12.0
|
558
588
|
#
|
559
589
|
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
590
|
+
if not self.__hash_utils_instance_respond_to? :deep_merge!
|
591
|
+
def deep_merge!(*args)
|
592
|
+
fm = args.map { |hash| [self, hash] }
|
593
|
+
|
594
|
+
while not fm.empty?
|
595
|
+
_in, _out = fm.shift
|
596
|
+
_out.each_pair do |k, v|
|
597
|
+
if v.kind_of? Hash
|
598
|
+
fm << [_in[k], _out[k]]
|
599
|
+
else
|
600
|
+
_in[k] = v
|
601
|
+
end
|
570
602
|
end
|
571
603
|
end
|
604
|
+
|
605
|
+
return self
|
572
606
|
end
|
573
|
-
|
574
|
-
return self
|
575
607
|
end
|
576
608
|
|
577
609
|
##
|
@@ -584,25 +616,27 @@ class Hash
|
|
584
616
|
# @since 0.12.0
|
585
617
|
#
|
586
618
|
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
619
|
+
if not self.__hash_utils_instance_respond_to? :deep_merge
|
620
|
+
def deep_merge(*args)
|
621
|
+
result = self.dup
|
622
|
+
fm = args.map { |hash| [result, hash] }
|
623
|
+
|
624
|
+
while not fm.empty?
|
625
|
+
_in, _out = fm.shift
|
626
|
+
_out.each_pair do |k, v|
|
627
|
+
if _in[k].kind_of? Hash
|
628
|
+
_in[k] = _in[k].dup
|
629
|
+
fm << [_in[k], _out[k]]
|
630
|
+
else
|
631
|
+
_in[k] = v
|
632
|
+
end
|
599
633
|
end
|
600
634
|
end
|
635
|
+
|
636
|
+
return result
|
601
637
|
end
|
602
|
-
|
603
|
-
return result
|
604
638
|
end
|
605
|
-
|
639
|
+
|
606
640
|
##
|
607
641
|
# Iterates through items with given key only. None-existing values
|
608
642
|
# are ignored.
|
@@ -612,10 +646,12 @@ class Hash
|
|
612
646
|
# @since 0.15.0
|
613
647
|
#
|
614
648
|
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
649
|
+
if not self.__hash_utils_instance_respond_to? :get_pairs
|
650
|
+
def get_pairs(*args)
|
651
|
+
self.take_pairs(*args) do |i|
|
652
|
+
if not i[1].nil?
|
653
|
+
yield i
|
654
|
+
end
|
619
655
|
end
|
620
656
|
end
|
621
657
|
end
|
@@ -634,13 +670,15 @@ class Hash
|
|
634
670
|
# @since 0.15.0
|
635
671
|
#
|
636
672
|
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
673
|
+
if not self.__hash_utils_instance_respond_to? :get_items
|
674
|
+
def get_items(*args)
|
675
|
+
result = { }
|
676
|
+
self.get_pairs(*args) do |key, value|
|
677
|
+
result[key] = value
|
678
|
+
end
|
679
|
+
|
680
|
+
return result
|
641
681
|
end
|
642
|
-
|
643
|
-
return result
|
644
682
|
end
|
645
683
|
|
646
684
|
##
|
@@ -654,14 +692,16 @@ class Hash
|
|
654
692
|
# @return [Array] array of values
|
655
693
|
# @since 0.15.0
|
656
694
|
#
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
695
|
+
|
696
|
+
if not self.__hash_utils_instance_respond_to? :get_values
|
697
|
+
def get_values(*args)
|
698
|
+
result = [ ]
|
699
|
+
self.get_pairs(*args) do |key, value|
|
700
|
+
result << value
|
701
|
+
end
|
702
|
+
|
703
|
+
return result
|
662
704
|
end
|
663
|
-
|
664
|
-
return result
|
665
705
|
end
|
666
706
|
|
667
707
|
##
|
@@ -673,9 +713,11 @@ class Hash
|
|
673
713
|
# @since 0.15.0
|
674
714
|
#
|
675
715
|
|
676
|
-
|
677
|
-
args
|
678
|
-
|
716
|
+
if not self.__hash_utils_instance_respond_to? :take_pairs
|
717
|
+
def take_pairs(*args)
|
718
|
+
args.each do |i|
|
719
|
+
yield [i, self[i]]
|
720
|
+
end
|
679
721
|
end
|
680
722
|
end
|
681
723
|
|
@@ -692,14 +734,16 @@ class Hash
|
|
692
734
|
# @return [Hash] new hash
|
693
735
|
# @since 0.15.0
|
694
736
|
#
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
737
|
+
|
738
|
+
if not self.__hash_utils_instance_respond_to? :take_items
|
739
|
+
def take_items(*args)
|
740
|
+
result = { }
|
741
|
+
self.take_pairs(*args) do |key, value|
|
742
|
+
result[key] = value
|
743
|
+
end
|
744
|
+
|
745
|
+
return result
|
700
746
|
end
|
701
|
-
|
702
|
-
return result
|
703
747
|
end
|
704
748
|
|
705
749
|
##
|
@@ -714,14 +758,16 @@ class Hash
|
|
714
758
|
# @return [Array] array of values
|
715
759
|
# @since 0.15.0
|
716
760
|
#
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
761
|
+
|
762
|
+
if not self.__hash_utils_instance_respond_to? :take_values
|
763
|
+
def take_values(*args)
|
764
|
+
result = [ ]
|
765
|
+
self.take_pairs(*args) do |key, value|
|
766
|
+
result << value
|
767
|
+
end
|
768
|
+
|
769
|
+
return result
|
722
770
|
end
|
723
|
-
|
724
|
-
return result
|
725
771
|
end
|
726
772
|
|
727
773
|
##
|
@@ -739,8 +785,10 @@ class Hash
|
|
739
785
|
# @since 0.16.0
|
740
786
|
#
|
741
787
|
|
742
|
-
|
743
|
-
|
788
|
+
if not self.__hash_utils_instance_respond_to? :sum
|
789
|
+
def sum
|
790
|
+
self.values.sum
|
791
|
+
end
|
744
792
|
end
|
745
793
|
|
746
794
|
##
|
@@ -755,32 +803,26 @@ class Hash
|
|
755
803
|
# @since 0.16.0
|
756
804
|
#
|
757
805
|
|
758
|
-
|
759
|
-
|
806
|
+
if not self.__hash_utils_instance_respond_to? :avg
|
807
|
+
def avg
|
808
|
+
self.values.avg
|
809
|
+
end
|
760
810
|
end
|
761
811
|
|
762
|
-
|
763
|
-
|
764
|
-
##
|
765
|
-
# Indicates, object is +Array+.
|
766
|
-
#
|
767
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
768
|
-
# @since 0.17.0
|
769
|
-
#
|
770
|
-
|
771
|
-
def array?
|
772
|
-
self.kind_of? Array
|
812
|
+
if not self.__hash_utils_instance_respond_to? :average
|
813
|
+
alias :average :avg
|
773
814
|
end
|
774
|
-
|
815
|
+
|
775
816
|
##
|
776
817
|
# Indicates, object is +Hash+.
|
777
818
|
#
|
778
819
|
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
779
820
|
# @since 0.17.0
|
780
821
|
#
|
781
|
-
|
782
|
-
|
783
|
-
|
822
|
+
if not self.__hash_utils_instance_respond_to? :hash?
|
823
|
+
def hash?
|
824
|
+
true
|
825
|
+
end
|
784
826
|
end
|
785
827
|
|
786
828
|
end
|