hash-utils 0.16.0 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -5
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/hash-utils.gemspec +4 -3
- data/lib/hash-utils.rb +1 -0
- data/lib/hash-utils/array.rb +11 -0
- data/lib/hash-utils/boolean.rb +86 -0
- data/lib/hash-utils/hash.rb +25 -0
- data/lib/hash-utils/numeric.rb +13 -1
- data/lib/hash-utils/object.rb +66 -0
- data/lib/hash-utils/string.rb +12 -0
- data/lib/hash-utils/symbol.rb +13 -0
- data/test +64 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
Hash Utils
|
2
2
|
==========
|
3
3
|
|
4
|
-
**hash-utils** adds more than
|
4
|
+
**hash-utils** adds more than 125 useful and frequently rather
|
5
5
|
fundamental methods which are missing in Ruby programming language,
|
6
6
|
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
10
|
- `Array` – 12 methods,
|
11
|
+
- `FalseClass` – 3 methods,
|
11
12
|
- `File` – 2 methods,
|
12
13
|
- `Hash` – 38 methods,
|
13
14
|
- `IO` – 1 method,
|
14
15
|
- `Module` – 1 method,
|
15
|
-
- `Numeric` –
|
16
|
-
- `Object` –
|
17
|
-
- `
|
16
|
+
- `Numeric` – 5 method,
|
17
|
+
- `Object` – 12 methods,
|
18
|
+
- `TrueClass` – 3 methods,
|
19
|
+
- `String` – 35 methods,
|
18
20
|
- `StringIO` – 1 method,
|
19
|
-
- `Symbol` –
|
21
|
+
- `Symbol` – 8 methods.
|
20
22
|
|
21
23
|
For full reference and methods lists, see **[documentation][3]**.
|
22
24
|
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
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
|
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
22
|
gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of\nconflict with built-in one with in fact equivalent functionallity. Please, check\nyour code if you can and switch to Ruby's one.\n\n"
|
23
23
|
gem.email = "martinkozak@martinkozak.net"
|
24
24
|
gem.authors = ["Martin Kozák"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.17.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.17.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Martin Kozák}]
|
12
|
-
s.date = %q{2011-06-
|
12
|
+
s.date = %q{2011-06-24}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"hash-utils.gemspec",
|
28
28
|
"lib/hash-utils.rb",
|
29
29
|
"lib/hash-utils/array.rb",
|
30
|
+
"lib/hash-utils/boolean.rb",
|
30
31
|
"lib/hash-utils/file.rb",
|
31
32
|
"lib/hash-utils/hash.rb",
|
32
33
|
"lib/hash-utils/io.rb",
|
@@ -48,7 +49,7 @@ your code if you can and switch to Ruby's one.
|
|
48
49
|
}
|
49
50
|
s.require_paths = [%q{lib}]
|
50
51
|
s.rubygems_version = %q{1.8.5}
|
51
|
-
s.summary = %q{Adds more than
|
52
|
+
s.summary = %q{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.}
|
52
53
|
|
53
54
|
if s.respond_to? :specification_version then
|
54
55
|
s.specification_version = 3
|
data/lib/hash-utils.rb
CHANGED
data/lib/hash-utils/array.rb
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
##
|
5
|
+
# +TrueClass+ extension.
|
6
|
+
# @since 0.17.0
|
7
|
+
#
|
8
|
+
|
9
|
+
class TrueClass
|
10
|
+
|
11
|
+
##
|
12
|
+
# Indicates, object is boolean.
|
13
|
+
#
|
14
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
15
|
+
# @since 0.17.0
|
16
|
+
#
|
17
|
+
|
18
|
+
def boolean?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Indicates, object is pure +false+, so +FalseClass+.
|
24
|
+
#
|
25
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
26
|
+
# @since 0.15.0
|
27
|
+
#
|
28
|
+
|
29
|
+
def false?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Indicates, object is pure +true+, so +TrueClass+.
|
35
|
+
#
|
36
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
37
|
+
# @since 0.15.0
|
38
|
+
#
|
39
|
+
|
40
|
+
def true?
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# +FalseClass+ extension.
|
48
|
+
# @since 0.17.0
|
49
|
+
#
|
50
|
+
|
51
|
+
class FalseClass
|
52
|
+
|
53
|
+
##
|
54
|
+
# Indicates, object is boolean.
|
55
|
+
#
|
56
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
57
|
+
# @since 0.17.0
|
58
|
+
#
|
59
|
+
|
60
|
+
def boolean?
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Indicates, object is pure +false+, so +FalseClass+.
|
66
|
+
#
|
67
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
68
|
+
# @since 0.15.0
|
69
|
+
#
|
70
|
+
|
71
|
+
def false?
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Indicates, object is pure +true+, so +TrueClass+.
|
77
|
+
#
|
78
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
79
|
+
# @since 0.15.0
|
80
|
+
#
|
81
|
+
|
82
|
+
def true?
|
83
|
+
false
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
data/lib/hash-utils/hash.rb
CHANGED
@@ -238,6 +238,8 @@ class Hash
|
|
238
238
|
end
|
239
239
|
end
|
240
240
|
|
241
|
+
alias :symbolize_keys :keys_to_sym
|
242
|
+
|
241
243
|
##
|
242
244
|
# Emulates {#keys_to_sym} on place. In fact, replaces old hash by
|
243
245
|
# new one.
|
@@ -250,6 +252,8 @@ class Hash
|
|
250
252
|
self.replace(self.keys_to_sym)
|
251
253
|
end
|
252
254
|
|
255
|
+
alias :"symbolize_keys!" :"keys_to_sym!"
|
256
|
+
|
253
257
|
##
|
254
258
|
# Checks, all elements values follow condition expressed in block.
|
255
259
|
# Block must return boolean.
|
@@ -730,6 +734,27 @@ class Hash
|
|
730
734
|
end
|
731
735
|
|
732
736
|
alias :average :avg
|
737
|
+
|
738
|
+
##
|
739
|
+
# Indicates, object is +Array+.
|
740
|
+
#
|
741
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
742
|
+
# @since 0.17.0
|
743
|
+
#
|
733
744
|
|
745
|
+
def array?
|
746
|
+
self.kind_of? Array
|
747
|
+
end
|
734
748
|
|
749
|
+
##
|
750
|
+
# Indicates, object is +Hash+.
|
751
|
+
#
|
752
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
753
|
+
# @since 0.17.0
|
754
|
+
#
|
755
|
+
|
756
|
+
def hash?
|
757
|
+
true
|
758
|
+
end
|
759
|
+
|
735
760
|
end
|
data/lib/hash-utils/numeric.rb
CHANGED
@@ -85,6 +85,18 @@ class Numeric
|
|
85
85
|
# @since 0.16.0
|
86
86
|
#
|
87
87
|
|
88
|
-
alias :"positive!" :abs
|
88
|
+
alias :"positive!" :abs
|
89
|
+
|
90
|
+
##
|
91
|
+
# Indicates, object is +Numeric+.
|
92
|
+
#
|
93
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
94
|
+
# @since 0.17.0
|
95
|
+
#
|
96
|
+
|
97
|
+
def number?
|
98
|
+
true
|
99
|
+
end
|
100
|
+
|
89
101
|
|
90
102
|
end
|
data/lib/hash-utils/object.rb
CHANGED
@@ -126,4 +126,70 @@ class Object
|
|
126
126
|
self.kind_of? FalseClass
|
127
127
|
end
|
128
128
|
|
129
|
+
##
|
130
|
+
# Indicates, object is +String+.
|
131
|
+
#
|
132
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
133
|
+
# @since 0.17.0
|
134
|
+
#
|
135
|
+
|
136
|
+
def string?
|
137
|
+
self.kind_of? String
|
138
|
+
end
|
139
|
+
|
140
|
+
##
|
141
|
+
# Indicates, object is +Symbol+.
|
142
|
+
#
|
143
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
144
|
+
# @since 0.17.0
|
145
|
+
#
|
146
|
+
|
147
|
+
def symbol?
|
148
|
+
self.kind_of? Symbol
|
149
|
+
end
|
150
|
+
|
151
|
+
##
|
152
|
+
# Indicates, object is +Numeric+.
|
153
|
+
#
|
154
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
155
|
+
# @since 0.17.0
|
156
|
+
#
|
157
|
+
|
158
|
+
def number?
|
159
|
+
self.kind_of? Numeric
|
160
|
+
end
|
161
|
+
|
162
|
+
##
|
163
|
+
# Indicates, object is boolean.
|
164
|
+
#
|
165
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
166
|
+
# @since 0.17.0
|
167
|
+
#
|
168
|
+
|
169
|
+
def boolean?
|
170
|
+
self.true? or self.false?
|
171
|
+
end
|
172
|
+
|
173
|
+
##
|
174
|
+
# Indicates, object is +Array+.
|
175
|
+
#
|
176
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
177
|
+
# @since 0.17.0
|
178
|
+
#
|
179
|
+
|
180
|
+
def array?
|
181
|
+
self.kind_of? Array
|
182
|
+
end
|
183
|
+
|
184
|
+
##
|
185
|
+
# Indicates, object is +Hash+.
|
186
|
+
#
|
187
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
188
|
+
# @since 0.17.0
|
189
|
+
#
|
190
|
+
|
191
|
+
def hash?
|
192
|
+
self.kind_of? Hash
|
193
|
+
end
|
194
|
+
|
129
195
|
end
|
data/lib/hash-utils/string.rb
CHANGED
@@ -571,6 +571,18 @@ class String
|
|
571
571
|
return self
|
572
572
|
end
|
573
573
|
|
574
|
+
##
|
575
|
+
# Indicates, object is +String+.
|
576
|
+
#
|
577
|
+
# @return [Boolean] +true+ if yes, +false+ in otherwise
|
578
|
+
# @since 0.17.0
|
579
|
+
#
|
580
|
+
|
581
|
+
def string?
|
582
|
+
true
|
583
|
+
end
|
584
|
+
|
585
|
+
|
574
586
|
|
575
587
|
private
|
576
588
|
|
data/lib/hash-utils/symbol.rb
CHANGED
@@ -111,5 +111,18 @@ class Symbol
|
|
111
111
|
i.to_sym
|
112
112
|
end
|
113
113
|
end
|
114
|
+
|
115
|
+
##
|
116
|
+
# Strips whitespace from symbol. It seems non-sense, but it's useful
|
117
|
+
# if you convert some data with problematic white-space heterogenity
|
118
|
+
# to symbols -- for example large CSV files or so.
|
119
|
+
#
|
120
|
+
# @return [Symbol] new symbol without whitespace
|
121
|
+
# @since 0.17.0
|
122
|
+
#
|
123
|
+
|
124
|
+
def strip
|
125
|
+
self.to_s.strip!.to_sym
|
126
|
+
end
|
114
127
|
|
115
128
|
end
|
data/test
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
4
4
|
|
5
5
|
$:.push("./lib")
|
6
|
+
$:.unshift("./lib")
|
7
|
+
|
6
8
|
require "stringio"
|
7
9
|
require "hash-utils"
|
8
10
|
require "riot"
|
@@ -40,11 +42,28 @@ context "Array" do
|
|
40
42
|
asserts("#avg") do
|
41
43
|
[1, 2, 3].avg == 2
|
42
44
|
end
|
45
|
+
asserts("#array?") do
|
46
|
+
[1, 2, 3].array?
|
47
|
+
end
|
43
48
|
asserts("#sum") do
|
44
49
|
[1, 2, 3].sum == 6
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
53
|
+
## FALSECLASS
|
54
|
+
|
55
|
+
context "FalseClass" do
|
56
|
+
asserts("#boolean?") do
|
57
|
+
false.boolean?
|
58
|
+
end
|
59
|
+
asserts("#false?") do
|
60
|
+
false.false?
|
61
|
+
end
|
62
|
+
asserts("#true?") do
|
63
|
+
not false.true?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
48
67
|
## FILE
|
49
68
|
|
50
69
|
context "File" do
|
@@ -97,6 +116,9 @@ context "Hash" do
|
|
97
116
|
h = { :a => 1, :b => 2, :c => 3 }
|
98
117
|
h.get_values(:a, :c, :d) == [1, 3]
|
99
118
|
end
|
119
|
+
asserts("#get_values") do
|
120
|
+
{ :a => 1, :b => 2, :c => 3 }.hash?
|
121
|
+
end
|
100
122
|
asserts("#map_values") do
|
101
123
|
topic.map_values { |i| i + 1 } == { :a => 2, :b => 3 }
|
102
124
|
end
|
@@ -180,6 +202,9 @@ context "Numeric" do
|
|
180
202
|
asserts("#negative!") do
|
181
203
|
5.negative! == -5 and -5.negative! == -5
|
182
204
|
end
|
205
|
+
asserts("#number?") do
|
206
|
+
5.number?
|
207
|
+
end
|
183
208
|
asserts("#positive?") do
|
184
209
|
5.positive? and not -2.positive?
|
185
210
|
end
|
@@ -194,11 +219,20 @@ context "Object" do
|
|
194
219
|
asserts("#**") do
|
195
220
|
"ab" ** 5 == ["ab", "ab", "ab", "ab", "ab"]
|
196
221
|
end
|
222
|
+
asserts("#array?") do
|
223
|
+
not "".array? and [].array?
|
224
|
+
end
|
225
|
+
asserts("#boolean?") do
|
226
|
+
true.boolean? and false.boolean? and not "".boolean?
|
227
|
+
end
|
197
228
|
asserts("#false?") do
|
198
229
|
(false.false? == true) and ("string".false? == false)
|
199
230
|
end
|
231
|
+
asserts("#hash?") do
|
232
|
+
not "".hash? and {}.hash?
|
233
|
+
end
|
200
234
|
asserts("#in?") do
|
201
|
-
5.in? 1..7
|
235
|
+
5.in? 1..7 and not 9.in? 1..7
|
202
236
|
end
|
203
237
|
asserts("#instance_of_any?") do
|
204
238
|
"".instance_of_any? [String, Symbol] and not "".instance_of_any? [Integer, Float]
|
@@ -209,6 +243,15 @@ context "Object" do
|
|
209
243
|
asserts("#kind_of_any?") do
|
210
244
|
"".kind_of_any? [String, Symbol] and not "".kind_of_any? [Integer, Float]
|
211
245
|
end
|
246
|
+
asserts("#number?") do
|
247
|
+
not :abcd.number? and 5.number?
|
248
|
+
end
|
249
|
+
asserts("#string?") do
|
250
|
+
"".string? and not 5.string?
|
251
|
+
end
|
252
|
+
asserts("#symbol?") do
|
253
|
+
:abcd.symbol? and not 5.symbol?
|
254
|
+
end
|
212
255
|
asserts("#to_b") do
|
213
256
|
(nil.to_b === false) and ("ab".to_b === true)
|
214
257
|
end
|
@@ -330,6 +373,9 @@ context "String" do
|
|
330
373
|
res &= str.shift_lines(2) == ["a\n", "b\n"]
|
331
374
|
res &= str = "c\nd\n"
|
332
375
|
end
|
376
|
+
asserts("#string?") do
|
377
|
+
"abcd".string?
|
378
|
+
end
|
333
379
|
asserts("#ucfirst") do
|
334
380
|
str = "abcd"
|
335
381
|
str.ucfirst == "Abcd" and str == "abcd"
|
@@ -394,4 +440,21 @@ context "Symbol" do
|
|
394
440
|
asserts("#start_with?") do
|
395
441
|
:abcde.start_with? "ghi", "abc"
|
396
442
|
end
|
443
|
+
asserts("#strip") do
|
444
|
+
:" a ".strip == :a
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
## TRUECLASS
|
449
|
+
|
450
|
+
context "TrueClass" do
|
451
|
+
asserts("#boolean?") do
|
452
|
+
true.boolean?
|
453
|
+
end
|
454
|
+
asserts("#false?") do
|
455
|
+
not true.false?
|
456
|
+
end
|
457
|
+
asserts("#true?") do
|
458
|
+
true.true?
|
459
|
+
end
|
397
460
|
end
|
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.17.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-06-
|
13
|
+
date: 2011-06-24 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: ruby-version
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- hash-utils.gemspec
|
78
78
|
- lib/hash-utils.rb
|
79
79
|
- lib/hash-utils/array.rb
|
80
|
+
- lib/hash-utils/boolean.rb
|
80
81
|
- lib/hash-utils/file.rb
|
81
82
|
- lib/hash-utils/hash.rb
|
82
83
|
- lib/hash-utils/io.rb
|
@@ -103,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
104
|
requirements:
|
104
105
|
- - ">="
|
105
106
|
- !ruby/object:Gem::Version
|
106
|
-
hash:
|
107
|
+
hash: 3233431478987707592
|
107
108
|
segments:
|
108
109
|
- 0
|
109
110
|
version: "0"
|
@@ -119,6 +120,6 @@ rubyforge_project:
|
|
119
120
|
rubygems_version: 1.8.5
|
120
121
|
signing_key:
|
121
122
|
specification_version: 3
|
122
|
-
summary: Adds more than
|
123
|
+
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.
|
123
124
|
test_files: []
|
124
125
|
|