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/CHANGES.txt
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,11 +2,8 @@ GEM
|
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
4
|
git (1.2.5)
|
5
|
-
|
6
|
-
bundler (~> 1.0)
|
5
|
+
jeweler2 (2.0.9)
|
7
6
|
git (>= 1.2.5)
|
8
|
-
rake
|
9
|
-
rake (0.9.2)
|
10
7
|
riot (0.12.5)
|
11
8
|
rr
|
12
9
|
rr (1.0.4)
|
@@ -17,6 +14,6 @@ PLATFORMS
|
|
17
14
|
|
18
15
|
DEPENDENCIES
|
19
16
|
bundler (>= 1.0.0)
|
20
|
-
|
17
|
+
jeweler2 (>= 2.0.0)
|
21
18
|
riot (>= 0.12.3)
|
22
19
|
ruby-version
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ to several core classes. It tries to be similar project to
|
|
7
7
|
[Ruby Facets][1] on principle, but less complex, more practical,
|
8
8
|
non-atomic and organized by better way.
|
9
9
|
|
10
|
-
- `Array` –
|
10
|
+
- `Array` – 15 methods,
|
11
11
|
- `FalseClass` – 3 methods,
|
12
12
|
- `File` – 2 methods,
|
13
13
|
- `Gem` – 2 methods,
|
@@ -16,7 +16,7 @@ non-atomic and organized by better way.
|
|
16
16
|
- `Module` – 1 method,
|
17
17
|
- `NilClass` – 1 method,
|
18
18
|
- `Numeric` – 5 method,
|
19
|
-
- `Object` –
|
19
|
+
- `Object` – 14 methods,
|
20
20
|
- `Proc` – 1 method,
|
21
21
|
- `TrueClass` – 3 methods,
|
22
22
|
- `String` – 37 methods,
|
@@ -51,7 +51,7 @@ Contributing
|
|
51
51
|
Copyright
|
52
52
|
---------
|
53
53
|
|
54
|
-
Copyright © 2011 [Martin Kozák][10]. See `LICENSE.txt` for
|
54
|
+
Copyright © 2011 – 2012 [Martin Kozák][10]. See `LICENSE.txt` for
|
55
55
|
further details.
|
56
56
|
|
57
57
|
[1]: http://rubyworks.github.com/facets/
|
data/Rakefile
CHANGED
@@ -11,16 +11,15 @@ rescue Bundler::BundlerError => e
|
|
11
11
|
end
|
12
12
|
|
13
13
|
require 'rake'
|
14
|
-
require '
|
14
|
+
require 'jeweler2'
|
15
15
|
|
16
16
|
Jeweler::Tasks.new do |gem|
|
17
17
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
18
18
|
gem.name = "hash-utils"
|
19
19
|
gem.homepage = "http://github.com/martinkozak/hash-utils"
|
20
20
|
gem.license = "MIT"
|
21
|
-
gem.summary = 'Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.'
|
22
|
-
|
23
|
-
gem.post_install_message = "\nHASH UTILS: File#write now works non-binary again. Binary writing were moved to File#binwrite.\n\n"
|
21
|
+
gem.summary = 'Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way. Thanks to defensive and careful patching it should be compatible with all other libraries.'
|
22
|
+
gem.post_install_message = "\nHASH UTILS: The 2.0 version avoids the method overwriting conflicts by not performing overwriting of already implemented methods. By this way, it can damage your existing applications based on older versions although it's very improbable. \n\n"
|
24
23
|
gem.email = "martinkozak@martinkozak.net"
|
25
24
|
gem.authors = ["Martin Kozák"]
|
26
25
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/hash-utils.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "hash-utils"
|
8
|
-
s.version = "
|
8
|
+
s.version = "2.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Koz\u{e1}k"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2012-03-15"
|
13
13
|
s.email = "martinkozak@martinkozak.net"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -45,10 +45,10 @@ Gem::Specification.new do |s|
|
|
45
45
|
]
|
46
46
|
s.homepage = "http://github.com/martinkozak/hash-utils"
|
47
47
|
s.licenses = ["MIT"]
|
48
|
-
s.post_install_message = "\nHASH UTILS:
|
48
|
+
s.post_install_message = "\nHASH UTILS: The 2.0 version avoids the method overwriting conflicts by not performing overwriting of already implemented methods. By this way, it can damage your existing applications based on older versions although it's very improbable. \n\n"
|
49
49
|
s.require_paths = ["lib"]
|
50
|
-
s.rubygems_version = "1.8.
|
51
|
-
s.summary = "Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way."
|
50
|
+
s.rubygems_version = "1.8.17"
|
51
|
+
s.summary = "Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way. Thanks to defensive and careful patching it should be compatible with all other libraries."
|
52
52
|
|
53
53
|
if s.respond_to? :specification_version then
|
54
54
|
s.specification_version = 3
|
@@ -56,18 +56,18 @@ Gem::Specification.new do |s|
|
|
56
56
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
57
|
s.add_runtime_dependency(%q<ruby-version>, [">= 0"])
|
58
58
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
59
|
-
s.add_development_dependency(%q<
|
59
|
+
s.add_development_dependency(%q<jeweler2>, [">= 2.0.0"])
|
60
60
|
s.add_development_dependency(%q<riot>, [">= 0.12.3"])
|
61
61
|
else
|
62
62
|
s.add_dependency(%q<ruby-version>, [">= 0"])
|
63
63
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
64
|
-
s.add_dependency(%q<
|
64
|
+
s.add_dependency(%q<jeweler2>, [">= 2.0.0"])
|
65
65
|
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
66
66
|
end
|
67
67
|
else
|
68
68
|
s.add_dependency(%q<ruby-version>, [">= 0"])
|
69
69
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
70
|
-
s.add_dependency(%q<
|
70
|
+
s.add_dependency(%q<jeweler2>, [">= 2.0.0"])
|
71
71
|
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
72
72
|
end
|
73
73
|
end
|
data/lib/hash-utils/array.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 "ruby-version"
|
5
6
|
|
6
7
|
##
|
@@ -9,8 +10,6 @@ require "ruby-version"
|
|
9
10
|
|
10
11
|
class Array
|
11
12
|
|
12
|
-
@__sum_check_done
|
13
|
-
|
14
13
|
##
|
15
14
|
# Moves selected values outside the array, so returns them.
|
16
15
|
#
|
@@ -22,18 +21,20 @@ class Array
|
|
22
21
|
# @since 0.3.0
|
23
22
|
#
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
if not self.__hash_utils_instance_respond_to? :remove!
|
25
|
+
def remove!(&block)
|
26
|
+
result = [ ]
|
27
|
+
self.reject! do |v|
|
28
|
+
if block.call(v)
|
29
|
+
result << v
|
30
|
+
true
|
31
|
+
else
|
32
|
+
false
|
33
|
+
end
|
33
34
|
end
|
35
|
+
|
36
|
+
return result
|
34
37
|
end
|
35
|
-
|
36
|
-
return result
|
37
38
|
end
|
38
39
|
|
39
40
|
##
|
@@ -45,7 +46,9 @@ class Array
|
|
45
46
|
# @since 0.2.0
|
46
47
|
#
|
47
48
|
|
48
|
-
|
49
|
+
if not self.__hash_utils_instance_respond_to? :some?
|
50
|
+
alias :"some?" :"one?"
|
51
|
+
end
|
49
52
|
|
50
53
|
##
|
51
54
|
# Converts array to +Hash+.
|
@@ -68,11 +71,13 @@ class Array
|
|
68
71
|
# @since 0.4.0
|
69
72
|
#
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
if not self.__hash_utils_instance_respond_to? :to_h
|
75
|
+
def to_h(mode = nil)
|
76
|
+
if mode == :flat
|
77
|
+
Hash[*self]
|
78
|
+
else
|
79
|
+
Hash[self]
|
80
|
+
end
|
76
81
|
end
|
77
82
|
end
|
78
83
|
|
@@ -92,10 +97,12 @@ class Array
|
|
92
97
|
# @since 0.10.0
|
93
98
|
#
|
94
99
|
|
95
|
-
|
96
|
-
arrays
|
97
|
-
|
98
|
-
|
100
|
+
if not self.__hash_utils_instance_respond_to? :merge!
|
101
|
+
def merge!(*arrays)
|
102
|
+
arrays.flatten! 1
|
103
|
+
arrays.each { |i| self << i }
|
104
|
+
self
|
105
|
+
end
|
99
106
|
end
|
100
107
|
|
101
108
|
##
|
@@ -105,8 +112,10 @@ class Array
|
|
105
112
|
# @since 0.15.0
|
106
113
|
#
|
107
114
|
|
108
|
-
|
109
|
-
|
115
|
+
if not self.__hash_utils_instance_respond_to? :second
|
116
|
+
def second
|
117
|
+
self[1]
|
118
|
+
end
|
110
119
|
end
|
111
120
|
|
112
121
|
##
|
@@ -116,8 +125,10 @@ class Array
|
|
116
125
|
# @since 0.15.0
|
117
126
|
#
|
118
127
|
|
119
|
-
|
120
|
-
|
128
|
+
if not self.__hash_utils_instance_respond_to? :third
|
129
|
+
def third
|
130
|
+
self[2]
|
131
|
+
end
|
121
132
|
end
|
122
133
|
|
123
134
|
##
|
@@ -126,9 +137,11 @@ class Array
|
|
126
137
|
# @return [Object] item of the array
|
127
138
|
# @since 0.15.0
|
128
139
|
#
|
129
|
-
|
130
|
-
|
131
|
-
|
140
|
+
|
141
|
+
if not self.__hash_utils_instance_respond_to? :fourth
|
142
|
+
def fourth
|
143
|
+
self[3]
|
144
|
+
end
|
132
145
|
end
|
133
146
|
|
134
147
|
##
|
@@ -138,8 +151,10 @@ class Array
|
|
138
151
|
# @since 0.15.0
|
139
152
|
#
|
140
153
|
|
141
|
-
|
142
|
-
|
154
|
+
if not self.__hash_utils_instance_respond_to? :fifth
|
155
|
+
def fifth
|
156
|
+
self[4]
|
157
|
+
end
|
143
158
|
end
|
144
159
|
|
145
160
|
##
|
@@ -148,9 +163,11 @@ class Array
|
|
148
163
|
# @return [Object] item of the array
|
149
164
|
# @since 0.15.0
|
150
165
|
#
|
151
|
-
|
152
|
-
|
153
|
-
|
166
|
+
|
167
|
+
if not self.__hash_utils_instance_respond_to? :sixth
|
168
|
+
def sixth
|
169
|
+
self[5]
|
170
|
+
end
|
154
171
|
end
|
155
172
|
|
156
173
|
##
|
@@ -160,8 +177,10 @@ class Array
|
|
160
177
|
# @since 0.15.0
|
161
178
|
#
|
162
179
|
|
163
|
-
|
164
|
-
|
180
|
+
if not self.__hash_utils_instance_respond_to? :seventh
|
181
|
+
def seventh
|
182
|
+
self[6]
|
183
|
+
end
|
165
184
|
end
|
166
185
|
|
167
186
|
##
|
@@ -171,54 +190,41 @@ class Array
|
|
171
190
|
# @since 0.15.0
|
172
191
|
#
|
173
192
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
if Ruby::Version >= [1, 8, 7]
|
179
|
-
|
180
|
-
##
|
181
|
-
# Returns sum of items in the array. If other than numeric type found,
|
182
|
-
# it will be normally conacenated if it's possible.
|
183
|
-
#
|
184
|
-
# @example
|
185
|
-
# array = [1, 2, 3]
|
186
|
-
# array.sum # will return 6
|
187
|
-
# array = ["a", "b", "c"]
|
188
|
-
# array.sum # will return "abc"
|
189
|
-
#
|
190
|
-
# @return [Object] result of summing
|
191
|
-
# @since 0.16.0
|
192
|
-
#
|
193
|
-
|
194
|
-
def sum
|
195
|
-
self.inject(:+)
|
193
|
+
if not self.__hash_utils_instance_respond_to? :eight
|
194
|
+
def eighth
|
195
|
+
self[7]
|
196
196
|
end
|
197
|
-
|
198
|
-
|
197
|
+
end
|
198
|
+
|
199
|
+
##
|
200
|
+
# Returns sum of items in the array. If other than numeric type found,
|
201
|
+
# it will be normally conacenated if it's possible.
|
202
|
+
#
|
203
|
+
# @example
|
204
|
+
# array = [1, 2, 3]
|
205
|
+
# array.sum # will return 6
|
206
|
+
# array = ["a", "b", "c"]
|
207
|
+
# array.sum # will return "abc"
|
208
|
+
#
|
209
|
+
# @return [Object] result of summing
|
210
|
+
# @since 0.16.0
|
211
|
+
#
|
199
212
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
first = true
|
216
|
-
self.inject(self.first) do |sum, i|
|
217
|
-
if first
|
218
|
-
first = false
|
219
|
-
sum
|
220
|
-
else
|
221
|
-
sum += i
|
213
|
+
if not self.__hash_utils_instance_respond_to? :sum
|
214
|
+
if Ruby::Version >= [1, 8, 7]
|
215
|
+
def sum
|
216
|
+
self.inject(:+)
|
217
|
+
end
|
218
|
+
else
|
219
|
+
def sum
|
220
|
+
first = true
|
221
|
+
self.inject(self.first) do |sum, i|
|
222
|
+
if first
|
223
|
+
first = false
|
224
|
+
sum
|
225
|
+
else
|
226
|
+
sum += i
|
227
|
+
end
|
222
228
|
end
|
223
229
|
end
|
224
230
|
end
|
@@ -236,11 +242,15 @@ class Array
|
|
236
242
|
# @since 0.16.0
|
237
243
|
#
|
238
244
|
|
239
|
-
|
240
|
-
|
245
|
+
if not self.__hash_utils_instance_respond_to? :avg
|
246
|
+
def avg
|
247
|
+
self.sum.to_f / self.length
|
248
|
+
end
|
241
249
|
end
|
242
250
|
|
243
|
-
|
251
|
+
if not self.__hash_utils_instance_respond_to? :average
|
252
|
+
alias :average :avg
|
253
|
+
end
|
244
254
|
|
245
255
|
##
|
246
256
|
# Indicates, object is +Array+.
|
@@ -249,8 +259,10 @@ class Array
|
|
249
259
|
# @since 0.17.0
|
250
260
|
#
|
251
261
|
|
252
|
-
|
253
|
-
|
262
|
+
if not self.__hash_utils_instance_respond_to? :array?
|
263
|
+
def array?
|
264
|
+
true
|
265
|
+
end
|
254
266
|
end
|
255
267
|
|
256
268
|
##
|
@@ -262,8 +274,10 @@ class Array
|
|
262
274
|
# @since 0.19.0
|
263
275
|
#
|
264
276
|
|
265
|
-
|
266
|
-
|
277
|
+
if not self.__hash_utils_instance_respond_to? :clean
|
278
|
+
def clean(value = nil)
|
279
|
+
self.reject { |v| v === value }
|
280
|
+
end
|
267
281
|
end
|
268
282
|
|
269
283
|
##
|
@@ -275,9 +289,23 @@ class Array
|
|
275
289
|
# @since 0.19.0
|
276
290
|
#
|
277
291
|
|
278
|
-
|
279
|
-
|
292
|
+
if not self.__hash_utils_instance_respond_to? :clean!
|
293
|
+
def clean!(value = nil)
|
294
|
+
self.reject! { |v| v === value }
|
295
|
+
end
|
280
296
|
end
|
281
297
|
|
298
|
+
##
|
299
|
+
# Converts array to set. Include sets before calling.
|
300
|
+
#
|
301
|
+
# @return [Set] new set
|
302
|
+
# @since 2.0.0
|
303
|
+
#
|
304
|
+
|
305
|
+
if not self.__hash_utils_instance_respond_to? :to_set
|
306
|
+
def to_set
|
307
|
+
Set::new(self)
|
308
|
+
end
|
309
|
+
end
|
282
310
|
end
|
283
311
|
|