hash-utils 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +32 -7
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/hash-utils.gemspec +9 -4
- data/lib/hash-utils.rb +5 -203
- data/lib/hash-utils/array.rb +63 -0
- data/lib/hash-utils/hash.rb +229 -0
- data/lib/hash-utils/numeric.rb +12 -0
- data/lib/hash-utils/string.rb +30 -0
- data/lib/hash-utils/symbol.rb +12 -0
- metadata +10 -5
data/Gemfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
# Add dependencies required to use your gem here.
|
3
3
|
# Example:
|
4
|
-
# gem "
|
4
|
+
# gem "multitype-introspection", ">= 0.1.0"
|
5
5
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
data/README.md
CHANGED
@@ -12,6 +12,7 @@ class to [Hash][2] class. Extends them with following methods:
|
|
12
12
|
* `#map_keys!`
|
13
13
|
* `#keys_to_sym` – converts all keys to [Symbols][6],
|
14
14
|
* `#keys_to_sym!`
|
15
|
+
* …
|
15
16
|
|
16
17
|
All methods with `!` emulates work *in place*, but in fact they
|
17
18
|
replace old hash with new one. An example of use:
|
@@ -25,16 +26,38 @@ replace old hash with new one. An example of use:
|
|
25
26
|
This is the same as `#keys_to_sym!` for example. And also introduces
|
26
27
|
some methods known from Python to both [Array][1] and [Hash][2] class:
|
27
28
|
|
28
|
-
* `some?` –
|
29
|
+
* `some?` – returns `true` if *some* element follows condition in block,
|
29
30
|
* `some_pairs?`
|
30
|
-
* `all?` –
|
31
|
+
* `all?` – returns `true` if *all* element follows condition in block,
|
31
32
|
* `all_pairs?`
|
32
33
|
|
33
34
|
For example:
|
34
35
|
|
35
36
|
foo = [:alfa, :beta, 5, :gama]
|
36
|
-
foo.some? { |i| i.kind_of?
|
37
|
+
foo.some? { |i| i.kind_of? Numeric } # returns true
|
37
38
|
foo.all? { |i| i.kind_of? Symbol } # returns false
|
39
|
+
|
40
|
+
And finally introduces one method to [Numeric][7], [Symbol][6] and
|
41
|
+
[String][8]:
|
42
|
+
|
43
|
+
* `in?` – returns `true` if value is in some Object which support
|
44
|
+
`include?` method
|
45
|
+
|
46
|
+
For example:
|
47
|
+
|
48
|
+
foo = [1, 2, 3, 5, 8]
|
49
|
+
5.in? foo # returns true
|
50
|
+
5.in? 3..6 # returns true
|
51
|
+
|
52
|
+
As you can see, it's syntactic sugar, of sure, but useful in some cases.
|
53
|
+
Except these, it adds many other useful metods. See documentation.
|
54
|
+
|
55
|
+
### Modularization
|
56
|
+
|
57
|
+
If you don't want to include whole library, you can include extensions
|
58
|
+
to appropriate type only by including the:
|
59
|
+
|
60
|
+
require "hash-utils/<lowercase-data-type-name>"
|
38
61
|
|
39
62
|
Contributing
|
40
63
|
------------
|
@@ -43,14 +66,14 @@ Contributing
|
|
43
66
|
2. Create a branch (`git checkout -b 20101220-my-change`).
|
44
67
|
3. Commit your changes (`git commit -am "Added something"`).
|
45
68
|
4. Push to the branch (`git push origin 20101220-my-change`).
|
46
|
-
5. Create an [Issue][
|
69
|
+
5. Create an [Issue][9] with a link to your branch.
|
47
70
|
6. Enjoy a refreshing Diet Coke and wait.
|
48
71
|
|
49
72
|
|
50
73
|
Copyright
|
51
74
|
---------
|
52
75
|
|
53
|
-
Copyright © 2011 [Martin Kozák][
|
76
|
+
Copyright © 2011 [Martin Kozák][10]. See `LICENSE.txt` for
|
54
77
|
further details.
|
55
78
|
|
56
79
|
[1]: http://www.ruby-doc.org/core/classes/Array.html
|
@@ -59,5 +82,7 @@ further details.
|
|
59
82
|
[4]: http://www.ruby-doc.org/core/classes/Array.html#M000279
|
60
83
|
[5]: http://www.ruby-doc.org/core/classes/Array.html#M000249
|
61
84
|
[6]: http://www.ruby-doc.org/core/classes/Symbol.html
|
62
|
-
[7]: http://
|
63
|
-
[8]: http://www.
|
85
|
+
[7]: http://www.ruby-doc.org/core/classes/Numeric.html
|
86
|
+
[8]: http://www.ruby-doc.org/core/classes/String.html
|
87
|
+
[9]: http://github.com/martinkozak/hash-utils/issues
|
88
|
+
[10]: http://www.martinkozak.net/
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
|
|
16
16
|
gem.name = "hash-utils"
|
17
17
|
gem.homepage = "http://github.com/martinkozak/hash-utils"
|
18
18
|
gem.license = "MIT"
|
19
|
-
gem.summary = 'Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them.'
|
19
|
+
gem.summary = 'Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.'
|
20
20
|
gem.email = "martinkozak@martinkozak.net"
|
21
21
|
gem.authors = ["Martin Kozák"]
|
22
22
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.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.3.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-01-
|
12
|
+
s.date = %q{2011-01-12}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -24,13 +24,18 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"hash-utils.gemspec",
|
27
|
-
"lib/hash-utils.rb"
|
27
|
+
"lib/hash-utils.rb",
|
28
|
+
"lib/hash-utils/array.rb",
|
29
|
+
"lib/hash-utils/hash.rb",
|
30
|
+
"lib/hash-utils/numeric.rb",
|
31
|
+
"lib/hash-utils/string.rb",
|
32
|
+
"lib/hash-utils/symbol.rb"
|
28
33
|
]
|
29
34
|
s.homepage = %q{http://github.com/martinkozak/hash-utils}
|
30
35
|
s.licenses = ["MIT"]
|
31
36
|
s.require_paths = ["lib"]
|
32
37
|
s.rubygems_version = %q{1.3.7}
|
33
|
-
s.summary = %q{Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them.}
|
38
|
+
s.summary = %q{Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.}
|
34
39
|
|
35
40
|
if s.respond_to? :specification_version then
|
36
41
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/lib/hash-utils.rb
CHANGED
@@ -1,206 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
|
11
|
-
def compact
|
12
|
-
self.reject { |k, v| v.nil? }
|
13
|
-
end
|
14
|
-
|
15
|
-
##
|
16
|
-
# Removes <tt>nil</tt> elements from the hash. Returns <tt>nil</tt>
|
17
|
-
# if no changes were made, otherwise returns <tt>self</self>.
|
18
|
-
#
|
19
|
-
|
20
|
-
def compact!
|
21
|
-
self.reject! { |k, v| v.nil? }
|
22
|
-
end
|
23
|
-
|
24
|
-
##
|
25
|
-
# Returns a new hash with the results of running block once for
|
26
|
-
# every pair in <tt>self</tt>.
|
27
|
-
#
|
28
|
-
|
29
|
-
def map_pairs(&block)
|
30
|
-
new = Hash::new(&self.default_proc)
|
31
|
-
new.default = self.default
|
32
|
-
|
33
|
-
self.each_pair do |k, v|
|
34
|
-
new_k, new_v = block.call(k, v)
|
35
|
-
new[new_k] = new_v
|
36
|
-
end
|
37
|
-
|
38
|
-
return new
|
39
|
-
end
|
40
|
-
|
41
|
-
alias :collect_pairs :map_pairs
|
42
|
-
|
43
|
-
##
|
44
|
-
# Emulates #map_pairs on place. In fact, replaces old hash by
|
45
|
-
# new one.
|
46
|
-
#
|
47
|
-
|
48
|
-
def map_pairs!(&block)
|
49
|
-
self.replace(self.map_pairs(&block))
|
50
|
-
end
|
51
|
-
|
52
|
-
alias :"collect_pairs!" :"map_pairs!"
|
53
|
-
|
54
|
-
##
|
55
|
-
# Returns a new hash with the results of running block once for
|
56
|
-
# every key in <tt>self</tt>.
|
57
|
-
#
|
58
|
-
|
59
|
-
def map_keys(&block)
|
60
|
-
self.map_pairs do |k, v|
|
61
|
-
[block.call(k), v]
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
alias :collect_keys :map_keys
|
66
|
-
|
67
|
-
##
|
68
|
-
# Emulates #map_keys on place. In fact, replaces old hash by
|
69
|
-
# new one.
|
70
|
-
#
|
71
|
-
|
72
|
-
def map_keys!(&block)
|
73
|
-
self.replace(self.map_keys(&block))
|
74
|
-
end
|
75
|
-
|
76
|
-
alias :"collect_keys!" :"map_keys!"
|
77
|
-
|
78
|
-
##
|
79
|
-
# Converts all keys to symbols.
|
80
|
-
#
|
81
|
-
|
82
|
-
def keys_to_sym
|
83
|
-
self.map_keys { |k| k.to_sym }
|
84
|
-
end
|
85
|
-
|
86
|
-
##
|
87
|
-
# Emulates #keys_to_sym on place. In fact, replaces old hash by
|
88
|
-
# new one.
|
89
|
-
#
|
90
|
-
|
91
|
-
def keys_to_sym!
|
92
|
-
self.replace(self.keys_to_sym)
|
93
|
-
end
|
94
|
-
|
95
|
-
##
|
96
|
-
# Checks, all elements values follow condition expressed in block.
|
97
|
-
# Block must return Boolean.
|
98
|
-
#
|
99
|
-
# If it's empty, returns <tt>true</tt>.
|
100
|
-
#
|
101
|
-
|
102
|
-
def all?(&block)
|
103
|
-
if self.empty?
|
104
|
-
return true
|
105
|
-
end
|
106
|
-
|
107
|
-
self.each_value do |v|
|
108
|
-
if block.call(v) == false
|
109
|
-
return false
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
return true
|
114
|
-
end
|
115
|
-
|
116
|
-
##
|
117
|
-
# Checks, all elements follow condition expressed in block.
|
118
|
-
# Block must return Boolean.
|
119
|
-
#
|
120
|
-
# If it's empty, returns <tt>true</tt>.
|
121
|
-
#
|
122
|
-
|
123
|
-
def all_pairs?(&block)
|
124
|
-
if self.empty?
|
125
|
-
return true
|
126
|
-
end
|
127
|
-
|
128
|
-
self.each_pair do |k, v|
|
129
|
-
if block.call(k, v) == false
|
130
|
-
return false
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
return true
|
135
|
-
end
|
136
|
-
|
137
|
-
##
|
138
|
-
# Checks, at least one element value follows condition expressed in
|
139
|
-
# block. Block must return Boolean.
|
140
|
-
#
|
141
|
-
|
142
|
-
def some?(&block)
|
143
|
-
self.each_value do |v|
|
144
|
-
if block.call(v) == true
|
145
|
-
return true
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
return false
|
150
|
-
end
|
151
|
-
|
152
|
-
##
|
153
|
-
# Checks, at least one element follows condition expressed in
|
154
|
-
# block. Block must return Boolean.
|
155
|
-
#
|
156
|
-
|
157
|
-
def some_pairs?(&block)
|
158
|
-
self.each_pair do |k, v|
|
159
|
-
if block.call(k, v) == true
|
160
|
-
return true
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
return false
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
class Array
|
169
|
-
|
170
|
-
##
|
171
|
-
# Checks, all values follow condition expressed in block.
|
172
|
-
# Block must return Boolean.
|
173
|
-
#
|
174
|
-
# If it's empty, returns <tt>true</tt>.
|
175
|
-
#
|
176
|
-
|
177
|
-
def all?(&block)
|
178
|
-
if self.empty?
|
179
|
-
return true
|
180
|
-
end
|
181
|
-
|
182
|
-
self.each do |v|
|
183
|
-
if block.call(v) == false
|
184
|
-
return false
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
return true
|
189
|
-
end
|
190
|
-
|
191
|
-
##
|
192
|
-
# Checks, at least one value follows condition expressed in
|
193
|
-
# block. Block must return Boolean.
|
194
|
-
#
|
195
|
-
|
196
|
-
def some?(&block)
|
197
|
-
self.each do |v|
|
198
|
-
if block.call(v) == true
|
199
|
-
return true
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
return false
|
204
|
-
end
|
205
|
-
|
206
|
-
end
|
4
|
+
require "hash-utils/array"
|
5
|
+
require "hash-utils/hash"
|
6
|
+
require "hash-utils/numeric"
|
7
|
+
require "hash-utils/string"
|
8
|
+
require "hash-utils/symbol"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
class Array
|
5
|
+
|
6
|
+
##
|
7
|
+
# Moves selected values outside the array, so returns them.
|
8
|
+
#
|
9
|
+
# Works similar to #reject!, but returns removed items instead
|
10
|
+
# of remaining items.
|
11
|
+
#
|
12
|
+
|
13
|
+
def remove!(&block)
|
14
|
+
result = [ ]
|
15
|
+
self.reject! do |v|
|
16
|
+
if block.call(v)
|
17
|
+
result << v
|
18
|
+
true
|
19
|
+
else
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
return result
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Checks, all values follow condition expressed in block.
|
29
|
+
# Block must return Boolean.
|
30
|
+
#
|
31
|
+
# If it's empty, returns <tt>true</tt>.
|
32
|
+
#
|
33
|
+
|
34
|
+
def all?(&block)
|
35
|
+
if self.empty?
|
36
|
+
return true
|
37
|
+
end
|
38
|
+
|
39
|
+
self.each do |v|
|
40
|
+
if block.call(v) == false
|
41
|
+
return false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
return true
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Checks, at least one value follows condition expressed in
|
50
|
+
# block. Block must return Boolean.
|
51
|
+
#
|
52
|
+
|
53
|
+
def some?(&block)
|
54
|
+
self.each do |v|
|
55
|
+
if block.call(v) == true
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
class Hash
|
5
|
+
|
6
|
+
##
|
7
|
+
# Defines hash by setting the default value or Proc and content.
|
8
|
+
#
|
9
|
+
|
10
|
+
def self.define(values = { }, default = nil, &block)
|
11
|
+
hash = self[values]
|
12
|
+
self::create(default, hash, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Creates hash by setting default settings in one call.
|
17
|
+
#
|
18
|
+
|
19
|
+
def self.create(default = nil, hash = { }, &block)
|
20
|
+
hash.default = default
|
21
|
+
|
22
|
+
if not block.nil?
|
23
|
+
hash.default_proc = block
|
24
|
+
end
|
25
|
+
|
26
|
+
return hash
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Recreates the hash, so creates empty one and assigns
|
31
|
+
# the same default values.
|
32
|
+
#
|
33
|
+
|
34
|
+
def recreate
|
35
|
+
self.class::create(self.default, &self.default_proc)
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Recreates the hash in place, so creates empty one, assigns
|
40
|
+
# the same default values and replaces the old one.
|
41
|
+
#
|
42
|
+
|
43
|
+
def recreate!
|
44
|
+
self.replace(self.recreate)
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Moves selected pairs outside the hash, so returns them.
|
49
|
+
# Output hash has the same default settings.
|
50
|
+
#
|
51
|
+
|
52
|
+
def remove!(&block)
|
53
|
+
result = self.recreate
|
54
|
+
delete = [ ]
|
55
|
+
|
56
|
+
self.each_pair do |k, v|
|
57
|
+
if block.call(k, v)
|
58
|
+
result[k] = v
|
59
|
+
delete << k
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
delete.each do |k|
|
64
|
+
self.delete(k)
|
65
|
+
end
|
66
|
+
|
67
|
+
return result
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Returns a copy of <tt>self</tt> with all <tt>nil</tt>
|
72
|
+
# elements removed.
|
73
|
+
#
|
74
|
+
|
75
|
+
def compact
|
76
|
+
self.reject { |k, v| v.nil? }
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Removes <tt>nil</tt> elements from the hash. Returns <tt>nil</tt>
|
81
|
+
# if no changes were made, otherwise returns <tt>self</self>.
|
82
|
+
#
|
83
|
+
|
84
|
+
def compact!
|
85
|
+
self.reject! { |k, v| v.nil? }
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Returns a new hash with the results of running block once for
|
90
|
+
# every pair in <tt>self</tt>.
|
91
|
+
#
|
92
|
+
|
93
|
+
def map_pairs(&block)
|
94
|
+
new = self.recreate
|
95
|
+
|
96
|
+
self.each_pair do |k, v|
|
97
|
+
new_k, new_v = block.call(k, v)
|
98
|
+
new[new_k] = new_v
|
99
|
+
end
|
100
|
+
|
101
|
+
return new
|
102
|
+
end
|
103
|
+
|
104
|
+
alias :collect_pairs :map_pairs
|
105
|
+
|
106
|
+
##
|
107
|
+
# Emulates #map_pairs on place. In fact, replaces old hash by
|
108
|
+
# new one.
|
109
|
+
#
|
110
|
+
|
111
|
+
def map_pairs!(&block)
|
112
|
+
self.replace(self.map_pairs(&block))
|
113
|
+
end
|
114
|
+
|
115
|
+
alias :"collect_pairs!" :"map_pairs!"
|
116
|
+
|
117
|
+
##
|
118
|
+
# Returns a new hash with the results of running block once for
|
119
|
+
# every key in <tt>self</tt>.
|
120
|
+
#
|
121
|
+
|
122
|
+
def map_keys(&block)
|
123
|
+
self.map_pairs do |k, v|
|
124
|
+
[block.call(k), v]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
alias :collect_keys :map_keys
|
129
|
+
|
130
|
+
##
|
131
|
+
# Emulates #map_keys on place. In fact, replaces old hash by
|
132
|
+
# new one.
|
133
|
+
#
|
134
|
+
|
135
|
+
def map_keys!(&block)
|
136
|
+
self.replace(self.map_keys(&block))
|
137
|
+
end
|
138
|
+
|
139
|
+
alias :"collect_keys!" :"map_keys!"
|
140
|
+
|
141
|
+
##
|
142
|
+
# Converts all keys to symbols.
|
143
|
+
#
|
144
|
+
|
145
|
+
def keys_to_sym
|
146
|
+
self.map_keys { |k| k.to_sym }
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# Emulates #keys_to_sym on place. In fact, replaces old hash by
|
151
|
+
# new one.
|
152
|
+
#
|
153
|
+
|
154
|
+
def keys_to_sym!
|
155
|
+
self.replace(self.keys_to_sym)
|
156
|
+
end
|
157
|
+
|
158
|
+
##
|
159
|
+
# Checks, all elements values follow condition expressed in block.
|
160
|
+
# Block must return Boolean.
|
161
|
+
#
|
162
|
+
# If it's empty, returns <tt>true</tt>.
|
163
|
+
#
|
164
|
+
|
165
|
+
def all?(&block)
|
166
|
+
if self.empty?
|
167
|
+
return true
|
168
|
+
end
|
169
|
+
|
170
|
+
self.each_value do |v|
|
171
|
+
if block.call(v) == false
|
172
|
+
return false
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
return true
|
177
|
+
end
|
178
|
+
|
179
|
+
##
|
180
|
+
# Checks, all elements follow condition expressed in block.
|
181
|
+
# Block must return Boolean.
|
182
|
+
#
|
183
|
+
# If it's empty, returns <tt>true</tt>.
|
184
|
+
#
|
185
|
+
|
186
|
+
def all_pairs?(&block)
|
187
|
+
if self.empty?
|
188
|
+
return true
|
189
|
+
end
|
190
|
+
|
191
|
+
self.each_pair do |k, v|
|
192
|
+
if block.call(k, v) == false
|
193
|
+
return false
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
return true
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# Checks, at least one element value follows condition expressed in
|
202
|
+
# block. Block must return Boolean.
|
203
|
+
#
|
204
|
+
|
205
|
+
def some?(&block)
|
206
|
+
self.each_value do |v|
|
207
|
+
if block.call(v) == true
|
208
|
+
return true
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
return false
|
213
|
+
end
|
214
|
+
|
215
|
+
##
|
216
|
+
# Checks, at least one element follows condition expressed in
|
217
|
+
# block. Block must return Boolean.
|
218
|
+
#
|
219
|
+
|
220
|
+
def some_pairs?(&block)
|
221
|
+
self.each_pair do |k, v|
|
222
|
+
if block.call(k, v) == true
|
223
|
+
return true
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
return false
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
class String
|
5
|
+
##
|
6
|
+
# Holds numeric matcher.
|
7
|
+
#
|
8
|
+
|
9
|
+
NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
|
10
|
+
|
11
|
+
##
|
12
|
+
# Indicates string is in some object which supports #include?.
|
13
|
+
#
|
14
|
+
|
15
|
+
def in?(range)
|
16
|
+
range.include? self
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Indicates, string is numeric, so consists of numbers only.
|
21
|
+
#
|
22
|
+
|
23
|
+
def numeric?
|
24
|
+
if self.match(self.class::NUMERIC)
|
25
|
+
true
|
26
|
+
else
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Martin Koz\xC3\xA1k"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -66,6 +66,11 @@ files:
|
|
66
66
|
- VERSION
|
67
67
|
- hash-utils.gemspec
|
68
68
|
- lib/hash-utils.rb
|
69
|
+
- lib/hash-utils/array.rb
|
70
|
+
- lib/hash-utils/hash.rb
|
71
|
+
- lib/hash-utils/numeric.rb
|
72
|
+
- lib/hash-utils/string.rb
|
73
|
+
- lib/hash-utils/symbol.rb
|
69
74
|
has_rdoc: true
|
70
75
|
homepage: http://github.com/martinkozak/hash-utils
|
71
76
|
licenses:
|
@@ -80,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
85
|
requirements:
|
81
86
|
- - ">="
|
82
87
|
- !ruby/object:Gem::Version
|
83
|
-
hash:
|
88
|
+
hash: -3600041208124521153
|
84
89
|
segments:
|
85
90
|
- 0
|
86
91
|
version: "0"
|
@@ -98,6 +103,6 @@ rubyforge_project:
|
|
98
103
|
rubygems_version: 1.3.7
|
99
104
|
signing_key:
|
100
105
|
specification_version: 3
|
101
|
-
summary: Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them.
|
106
|
+
summary: Adds some useful well known methods similar to appropriate Array methods to Hash and some useful methods known from Python to both of them. Also defines some utility method for other data types.
|
102
107
|
test_files: []
|
103
108
|
|