hash-utils 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/hash-utils.gemspec +9 -2
- data/lib/hash-utils/array.rb +23 -34
- data/lib/hash-utils/hash.rb +45 -23
- data/test +1 -6
- metadata +8 -4
data/Rakefile
CHANGED
@@ -17,6 +17,7 @@ Jeweler::Tasks.new do |gem|
|
|
17
17
|
gem.homepage = "http://github.com/martinkozak/hash-utils"
|
18
18
|
gem.license = "MIT"
|
19
19
|
gem.summary = 'Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Object and String classes too.'
|
20
|
+
gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all? is deprecated since version 0.10.0 because of\nconflict with built-in one with in fact equivalent functionallity. It will be\nremoved around version 0.13.0. Please, check your code if you can and switch\nto Ruby's one.\n\n"
|
20
21
|
gem.email = "martinkozak@martinkozak.net"
|
21
22
|
gem.authors = ["Martin Kozák"]
|
22
23
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.10.0
|
data/hash-utils.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{hash-utils}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.10.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ák"]
|
12
|
-
s.date = %q{2011-02-
|
12
|
+
s.date = %q{2011-02-23}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -36,6 +36,13 @@ Gem::Specification.new do |s|
|
|
36
36
|
]
|
37
37
|
s.homepage = %q{http://github.com/martinkozak/hash-utils}
|
38
38
|
s.licenses = ["MIT"]
|
39
|
+
s.post_install_message = %q{
|
40
|
+
HASH UTILS: Be warn, Hash#all? is deprecated since version 0.10.0 because of
|
41
|
+
conflict with built-in one with in fact equivalent functionallity. It will be
|
42
|
+
removed around version 0.13.0. Please, check your code if you can and switch
|
43
|
+
to Ruby's one.
|
44
|
+
|
45
|
+
}
|
39
46
|
s.require_paths = ["lib"]
|
40
47
|
s.rubygems_version = %q{1.5.2}
|
41
48
|
s.summary = %q{Adds a lot of useful fundamental utility methods which are missing in Ruby, both to Array and Hash classes and introduces some useful methods and syntactic sugar to Object and String classes too.}
|
data/lib/hash-utils/array.rb
CHANGED
@@ -31,32 +31,7 @@ class Array
|
|
31
31
|
|
32
32
|
return result
|
33
33
|
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# Checks, all values follow condition expressed in block.
|
37
|
-
# Block must return Boolean.
|
38
|
-
#
|
39
|
-
# If it's empty, returns +true+.
|
40
|
-
#
|
41
|
-
# @param [Proc] block checking block
|
42
|
-
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
43
|
-
# @since 0.2.0
|
44
|
-
#
|
45
34
|
|
46
|
-
def all?(&block)
|
47
|
-
if self.empty?
|
48
|
-
return true
|
49
|
-
end
|
50
|
-
|
51
|
-
self.each do |v|
|
52
|
-
if block.call(v) == false
|
53
|
-
return false
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
return true
|
58
|
-
end
|
59
|
-
|
60
35
|
##
|
61
36
|
# Checks, at least one value follows condition expressed in
|
62
37
|
# block. Block must return Boolean.
|
@@ -66,15 +41,7 @@ class Array
|
|
66
41
|
# @since 0.2.0
|
67
42
|
#
|
68
43
|
|
69
|
-
|
70
|
-
self.each do |v|
|
71
|
-
if block.call(v) == true
|
72
|
-
return true
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
return false
|
77
|
-
end
|
44
|
+
alias :some? :one?
|
78
45
|
|
79
46
|
##
|
80
47
|
# Converts array to +Hash+.
|
@@ -105,4 +72,26 @@ class Array
|
|
105
72
|
end
|
106
73
|
end
|
107
74
|
|
75
|
+
##
|
76
|
+
# Merges arrays in place. It seems to be rather unuseful, but it's
|
77
|
+
# intended for special cases, for example joining arrays while
|
78
|
+
# constructing.
|
79
|
+
#
|
80
|
+
# An example (underlying object is extended array):
|
81
|
+
# def initialize(array)
|
82
|
+
# self += array # impossible, will fail
|
83
|
+
# self.merge! array # possible, of sure
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# @param [Array] arrays array for merge-in
|
87
|
+
# @return [Array] self
|
88
|
+
# @since 0.10.0
|
89
|
+
#
|
90
|
+
|
91
|
+
def merge!(*arrays)
|
92
|
+
arrays.flatten!(1)
|
93
|
+
arrays.each { |i| self << i }
|
94
|
+
self
|
95
|
+
end
|
96
|
+
|
108
97
|
end
|
data/lib/hash-utils/hash.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
-
|
5
4
|
##
|
6
5
|
# Hash extension.
|
7
6
|
#
|
@@ -218,11 +217,21 @@ class Hash
|
|
218
217
|
#
|
219
218
|
# @param [Proc] block checking block
|
220
219
|
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
220
|
+
# @note This method is currently in conflict with Ruby 1.9.2
|
221
|
+
# +Hash#all?+ method. Given block arity is checked, so code should
|
222
|
+
# be compatible for now, but this method will be probably moved
|
223
|
+
# around +0.13.0+ for performance reasons of sure to deprecated
|
224
|
+
# module.
|
225
|
+
# @deprecated (since 0.10.0, conflict with built-in method)
|
221
226
|
# @since 0.2.0
|
222
227
|
#
|
223
228
|
|
224
229
|
def all?(&block)
|
225
|
-
if
|
230
|
+
if block.arity == 2
|
231
|
+
self.all_pairs? &block
|
232
|
+
end
|
233
|
+
|
234
|
+
if self.empty? or block.nil?
|
226
235
|
return true
|
227
236
|
end
|
228
237
|
|
@@ -234,7 +243,7 @@ class Hash
|
|
234
243
|
|
235
244
|
return true
|
236
245
|
end
|
237
|
-
|
246
|
+
|
238
247
|
##
|
239
248
|
# Checks, all elements follow condition expressed in block.
|
240
249
|
# Block must return boolean.
|
@@ -243,6 +252,8 @@ class Hash
|
|
243
252
|
#
|
244
253
|
# @param [Proc] block checking block
|
245
254
|
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
255
|
+
# @note Since version 0.13.0 will be replaced by an alias to
|
256
|
+
# Ruby built-in +#all?+.
|
246
257
|
# @since 0.2.0
|
247
258
|
#
|
248
259
|
|
@@ -270,15 +281,11 @@ class Hash
|
|
270
281
|
#
|
271
282
|
|
272
283
|
def some?(&block)
|
273
|
-
self.
|
274
|
-
|
275
|
-
return true
|
276
|
-
end
|
284
|
+
self.one? do |pair|
|
285
|
+
block.call(pair[1])
|
277
286
|
end
|
278
|
-
|
279
|
-
return false
|
280
287
|
end
|
281
|
-
|
288
|
+
|
282
289
|
##
|
283
290
|
# Checks, at least one element follows condition expressed in
|
284
291
|
# block. Block must return boolean.
|
@@ -287,16 +294,8 @@ class Hash
|
|
287
294
|
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
288
295
|
# @since 0.2.0
|
289
296
|
#
|
290
|
-
|
291
|
-
|
292
|
-
self.each_pair do |k, v|
|
293
|
-
if block.call(k, v) == true
|
294
|
-
return true
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
return false
|
299
|
-
end
|
297
|
+
|
298
|
+
alias :some_pairs? :one?
|
300
299
|
|
301
300
|
##
|
302
301
|
# Compatibility method with {Array#to_h}. Returns itself.
|
@@ -305,9 +304,7 @@ class Hash
|
|
305
304
|
# @since 0.4.0
|
306
305
|
#
|
307
306
|
|
308
|
-
|
309
|
-
self
|
310
|
-
end
|
307
|
+
alias :to_h :to_hash
|
311
308
|
|
312
309
|
##
|
313
310
|
# Simulates sorting in place. In fact, replaces old one by new one.
|
@@ -451,4 +448,29 @@ class Hash
|
|
451
448
|
return false
|
452
449
|
end
|
453
450
|
|
451
|
+
##
|
452
|
+
# Combines two arrays to Hash. First array will be keys, second one
|
453
|
+
# will be values.
|
454
|
+
#
|
455
|
+
# @example
|
456
|
+
# keys = [:a, :b, :c]
|
457
|
+
# values = [1, 2, 3]
|
458
|
+
#
|
459
|
+
# Hash::combine(keys, values) # will return {:a => 1, :b => 2, :c => 3}
|
460
|
+
#
|
461
|
+
# @param [Array] keys keys
|
462
|
+
# @param [Array] values values
|
463
|
+
# @return [Hash] resultant hash
|
464
|
+
# @since 0.10.0
|
465
|
+
#
|
466
|
+
|
467
|
+
def self.combine(keys, values)
|
468
|
+
result = { }
|
469
|
+
keys.each_index do |i|
|
470
|
+
result[keys[i]] = values[i]
|
471
|
+
end
|
472
|
+
|
473
|
+
return result
|
474
|
+
end
|
475
|
+
|
454
476
|
end
|
data/test
CHANGED
@@ -3,9 +3,4 @@
|
|
3
3
|
$:.push("./lib")
|
4
4
|
require "hash-utils/hash"
|
5
5
|
|
6
|
-
|
7
|
-
puts hash.has_keys? [:a, :b]
|
8
|
-
puts hash.has_keys? [:a]
|
9
|
-
puts hash.has_keys? [:a, :c]
|
10
|
-
puts hash.has_some? [:a, :c]
|
11
|
-
puts hash.has_some? [:c]
|
6
|
+
puts Hash::combine([:a, :b, :c], [1, 2, 3]).inspect
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: hash-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.10.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Martin Koz\xC3\xA1k"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-23 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -66,7 +66,11 @@ has_rdoc: true
|
|
66
66
|
homepage: http://github.com/martinkozak/hash-utils
|
67
67
|
licenses:
|
68
68
|
- MIT
|
69
|
-
post_install_message:
|
69
|
+
post_install_message: "\n\
|
70
|
+
HASH UTILS: Be warn, Hash#all? is deprecated since version 0.10.0 because of\n\
|
71
|
+
conflict with built-in one with in fact equivalent functionallity. It will be\n\
|
72
|
+
removed around version 0.13.0. Please, check your code if you can and switch\n\
|
73
|
+
to Ruby's one.\n\n"
|
70
74
|
rdoc_options: []
|
71
75
|
|
72
76
|
require_paths:
|
@@ -76,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
80
|
requirements:
|
77
81
|
- - ">="
|
78
82
|
- !ruby/object:Gem::Version
|
79
|
-
hash:
|
83
|
+
hash: 1122397568412292267
|
80
84
|
segments:
|
81
85
|
- 0
|
82
86
|
version: "0"
|