hash-utils 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -5
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/hash-utils.gemspec +3 -3
- data/lib/hash-utils.rb +113 -1
- metadata +5 -5
data/README.md
CHANGED
@@ -2,7 +2,7 @@ Hash Utils
|
|
2
2
|
==========
|
3
3
|
|
4
4
|
**Hash Utils** adds some utility methods well known from [Array][1]
|
5
|
-
class to [Hash][2] class. Extends
|
5
|
+
class to [Hash][2] class. Extends them with following methods:
|
6
6
|
|
7
7
|
* [`#compact`][3] – rejects elements with `nil` values,
|
8
8
|
* [`#compact!`][4]
|
@@ -11,9 +11,9 @@ class to [Hash][2] class. Extends it with following methods:
|
|
11
11
|
* `#map_keys` – works as [Array#map][5], but for *keys only*,
|
12
12
|
* `#map_keys!`
|
13
13
|
* `#keys_to_sym` – converts all keys to [Symbols][6],
|
14
|
-
* `#keys_to_sym
|
14
|
+
* `#keys_to_sym!`
|
15
15
|
|
16
|
-
All methods with `!` emulates work *in place*, but in fact they
|
16
|
+
All methods with `!` emulates work *in place*, but in fact they
|
17
17
|
replace old hash with new one. An example of use:
|
18
18
|
|
19
19
|
foo = {"a" => 1, "b" => 2}
|
@@ -22,7 +22,19 @@ replace old hash with new one. An example of use:
|
|
22
22
|
# result will be
|
23
23
|
# {:a => 1, :b => 2}
|
24
24
|
|
25
|
-
This is the same as `#keys_to_sym!` for example.
|
25
|
+
This is the same as `#keys_to_sym!` for example. And also introduces
|
26
|
+
some methods known from Python to both [Array][1] and [Hash][2] class:
|
27
|
+
|
28
|
+
* `some?` – returns `true` if *some* element follows condition in block,
|
29
|
+
* `some_pairs?`
|
30
|
+
* `all?` – returns `true` if *all* element follows condition in block,
|
31
|
+
* `all_pairs?`
|
32
|
+
|
33
|
+
For example:
|
34
|
+
|
35
|
+
foo = [:alfa, :beta, 5, :gama]
|
36
|
+
foo.some? { |i| i.kind_of? Integer } # returns true
|
37
|
+
foo.all? { |i| i.kind_of? Symbol } # returns false
|
26
38
|
|
27
39
|
Contributing
|
28
40
|
------------
|
@@ -38,7 +50,7 @@ Contributing
|
|
38
50
|
Copyright
|
39
51
|
---------
|
40
52
|
|
41
|
-
Copyright
|
53
|
+
Copyright © 2011 [Martin Kozák][8]. See `LICENSE.txt` for
|
42
54
|
further details.
|
43
55
|
|
44
56
|
[1]: http://www.ruby-doc.org/core/classes/Array.html
|
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.'
|
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.'
|
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.2.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.2.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-04}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
s.licenses = ["MIT"]
|
31
31
|
s.require_paths = ["lib"]
|
32
32
|
s.rubygems_version = %q{1.3.7}
|
33
|
-
s.summary = %q{Adds some useful well known methods similar to appropriate Array methods to Hash.}
|
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.}
|
34
34
|
|
35
35
|
if s.respond_to? :specification_version then
|
36
36
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/lib/hash-utils.rb
CHANGED
@@ -24,7 +24,7 @@ class Hash
|
|
24
24
|
##
|
25
25
|
# Returns a new hash with the results of running block once for
|
26
26
|
# every pair in <tt>self</tt>.
|
27
|
-
#
|
27
|
+
#
|
28
28
|
|
29
29
|
def map_pairs(&block)
|
30
30
|
new = Hash::new(&self.default_proc)
|
@@ -91,4 +91,116 @@ class Hash
|
|
91
91
|
def keys_to_sym!
|
92
92
|
self.replace(self.keys_to_sym)
|
93
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
|
+
|
94
206
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.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-04 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
80
|
requirements:
|
81
81
|
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
hash:
|
83
|
+
hash: 3013449918055684649
|
84
84
|
segments:
|
85
85
|
- 0
|
86
86
|
version: "0"
|
@@ -98,6 +98,6 @@ rubyforge_project:
|
|
98
98
|
rubygems_version: 1.3.7
|
99
99
|
signing_key:
|
100
100
|
specification_version: 3
|
101
|
-
summary: Adds some useful well known methods similar to appropriate Array methods to Hash.
|
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.
|
102
102
|
test_files: []
|
103
103
|
|