hash-utils 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -5
- data/CHANGES.txt +13 -3
- data/Gemfile.lock +13 -13
- data/README.md +5 -4
- data/Rakefile +1 -2
- data/TODO.txt +0 -1
- data/VERSION +1 -1
- data/hash-utils.gemspec +17 -4
- data/lib/hash-utils/array.rb +276 -2
- data/lib/hash-utils/hash.rb +43 -9
- data/lib/hash-utils/object.rb +6 -2
- data/lib/hash-utils/string.rb +32 -11
- data/lib/hash-utils/symbol.rb +15 -0
- data/spec/modules/array_spec.rb +177 -0
- data/spec/modules/false_spec.rb +23 -0
- data/spec/modules/file_spec.rb +22 -0
- data/spec/modules/hash_spec.rb +223 -0
- data/spec/modules/io_spec.rb +17 -0
- data/spec/modules/module_spec.rb +12 -0
- data/spec/modules/nil_spec.rb +11 -0
- data/spec/modules/numeric_spec.rb +32 -0
- data/spec/modules/object_spec.rb +78 -0
- data/spec/modules/string_spec.rb +245 -0
- data/spec/modules/stringio_spec.rb +11 -0
- data/spec/modules/symbol_spec.rb +40 -0
- data/spec/modules/true_spec.rb +23 -0
- data/spec/spec_helper.rb +15 -0
- metadata +18 -5
- data/tests.rb +0 -556
data/lib/hash-utils/hash.rb
CHANGED
@@ -31,18 +31,44 @@ class Hash
|
|
31
31
|
##
|
32
32
|
# Creates hash by setting default settings in one call.
|
33
33
|
#
|
34
|
-
# @param [Hash] values initial values
|
35
34
|
# @param [Object] default default value
|
35
|
+
# @param [Hash] hash initial values
|
36
36
|
# @param [Proc] block default block
|
37
37
|
# @return [Hash] new hash
|
38
38
|
# @since 0.3.0
|
39
39
|
#
|
40
40
|
|
41
41
|
if not self.respond_to? :create
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
if Ruby::Version >= "1.9"
|
43
|
+
def self.create(default = nil, hash = nil, &block)
|
44
|
+
if not hash.nil?
|
45
|
+
hash = hash.dup
|
46
|
+
else
|
47
|
+
hash = { }
|
48
|
+
end
|
49
|
+
|
50
|
+
hash.default = default
|
51
|
+
hash.default_proc = block if not block.nil?
|
52
|
+
return hash
|
53
|
+
end
|
54
|
+
else
|
55
|
+
def self.create(default = nil, hash = nil, &block)
|
56
|
+
if not hash.nil?
|
57
|
+
hash = hash.dup
|
58
|
+
else
|
59
|
+
hash = { }
|
60
|
+
end
|
61
|
+
|
62
|
+
if not block.nil?
|
63
|
+
new = Hash::new(&block)
|
64
|
+
new.update(hash) if not hash.empty?
|
65
|
+
hash = new
|
66
|
+
else
|
67
|
+
hash.default = default
|
68
|
+
end
|
69
|
+
|
70
|
+
return hash
|
71
|
+
end
|
46
72
|
end
|
47
73
|
end
|
48
74
|
|
@@ -357,9 +383,11 @@ class Hash
|
|
357
383
|
|
358
384
|
if not self.__hash_utils_instance_respond_to? :some?
|
359
385
|
def some?(&block)
|
360
|
-
self.
|
361
|
-
block.call(
|
386
|
+
self.each_value do |v|
|
387
|
+
return true if block.call(v)
|
362
388
|
end
|
389
|
+
|
390
|
+
return false
|
363
391
|
end
|
364
392
|
end
|
365
393
|
|
@@ -373,7 +401,13 @@ class Hash
|
|
373
401
|
#
|
374
402
|
|
375
403
|
if not self.__hash_utils_instance_respond_to? :some_pairs?
|
376
|
-
|
404
|
+
def some_pairs?(&block)
|
405
|
+
self.each_pair do |pair|
|
406
|
+
return true if block.call(pair)
|
407
|
+
end
|
408
|
+
|
409
|
+
return false
|
410
|
+
end
|
377
411
|
end
|
378
412
|
|
379
413
|
##
|
@@ -383,7 +417,7 @@ class Hash
|
|
383
417
|
# @since 0.4.0
|
384
418
|
#
|
385
419
|
|
386
|
-
if not self.__hash_utils_instance_respond_to? :to_h
|
420
|
+
if (not self.__hash_utils_instance_respond_to? :to_h) or (Ruby::Version >= "2.0")
|
387
421
|
def to_h(mode = nil)
|
388
422
|
self
|
389
423
|
end
|
data/lib/hash-utils/object.rb
CHANGED
@@ -321,12 +321,16 @@ class Object
|
|
321
321
|
#
|
322
322
|
# @return [Symbol] symbol representation of the object
|
323
323
|
# @since 1.1.0
|
324
|
+
# @since 2.1.0 is +#to_sym+ deprecated in favor to +#to_symbol+
|
324
325
|
#
|
325
326
|
|
326
|
-
if not __hash_utils_object_respond_to? :
|
327
|
-
def
|
327
|
+
if not __hash_utils_object_respond_to? :to_symbol
|
328
|
+
def to_symbol
|
328
329
|
self.to_s.to_sym
|
329
330
|
end
|
330
331
|
end
|
331
332
|
|
333
|
+
if not __hash_utils_object_respond_to? :to_sym
|
334
|
+
alias :to_sym :to_symbol
|
335
|
+
end
|
332
336
|
end
|
data/lib/hash-utils/string.rb
CHANGED
@@ -215,13 +215,24 @@ class String
|
|
215
215
|
#
|
216
216
|
|
217
217
|
if not self.__hash_utils_instance_respond_to? :map
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
218
|
+
if Ruby::Version >= "1.9"
|
219
|
+
def map(&block)
|
220
|
+
buffer = " " * self.length
|
221
|
+
self.length.times do |i|
|
222
|
+
buffer[i] = block.call(self[i]).to_s
|
223
|
+
end
|
224
|
+
|
225
|
+
return buffer
|
226
|
+
end
|
227
|
+
else
|
228
|
+
def map(&block)
|
229
|
+
buffer = " " * self.length
|
230
|
+
self.length.times do |i|
|
231
|
+
buffer[i] = block.call(self[i]).to_i
|
232
|
+
end
|
233
|
+
|
234
|
+
return buffer
|
222
235
|
end
|
223
|
-
|
224
|
-
return buffer
|
225
236
|
end
|
226
237
|
end
|
227
238
|
|
@@ -235,12 +246,22 @@ class String
|
|
235
246
|
#
|
236
247
|
|
237
248
|
if not self.__hash_utils_instance_respond_to? :map!
|
238
|
-
|
239
|
-
|
240
|
-
self
|
249
|
+
if Ruby::Version >= "1.9"
|
250
|
+
def map!(&block)
|
251
|
+
self.length.times do |i|
|
252
|
+
self[i] = block.call(self[i]).to_s
|
253
|
+
end
|
254
|
+
|
255
|
+
return self
|
256
|
+
end
|
257
|
+
else
|
258
|
+
def map!(&block)
|
259
|
+
self.length.times do |i|
|
260
|
+
self[i] = block.call(self[i]).to_i
|
261
|
+
end
|
262
|
+
|
263
|
+
return self
|
241
264
|
end
|
242
|
-
|
243
|
-
return self
|
244
265
|
end
|
245
266
|
end
|
246
267
|
|
data/lib/hash-utils/symbol.rb
CHANGED
@@ -147,5 +147,20 @@ class Symbol
|
|
147
147
|
self.to_s.strip!.to_sym
|
148
148
|
end
|
149
149
|
end
|
150
|
+
|
151
|
+
##
|
152
|
+
# Compares symbols by three-value comparing. Implements the +<=>+
|
153
|
+
# operator in fact.
|
154
|
+
#
|
155
|
+
# @param [Symbol] symbol to compare
|
156
|
+
# @return [Integer]
|
157
|
+
# @since 2.1.0
|
158
|
+
#
|
159
|
+
|
160
|
+
if not self.__hash_utils_instance_respond_to? :<=>
|
161
|
+
def <=>(symbol)
|
162
|
+
self.to_s <=> symbol.to_s
|
163
|
+
end
|
164
|
+
end
|
150
165
|
|
151
166
|
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
4
|
+
|
5
|
+
require "spec_helper"
|
6
|
+
|
7
|
+
describe "Array" do
|
8
|
+
specify("#second") do
|
9
|
+
[1, 2, 3, 4, 5, 6, 7, 8].second.should eq(2)
|
10
|
+
end
|
11
|
+
specify("#third") do
|
12
|
+
[1, 2, 3, 4, 5, 6, 7, 8].third.should eq(3)
|
13
|
+
end
|
14
|
+
specify("#fourth") do
|
15
|
+
[1, 2, 3, 4, 5, 6, 7, 8].fourth.should eq(4)
|
16
|
+
end
|
17
|
+
specify("#fifth") do
|
18
|
+
[1, 2, 3, 4, 5, 6, 7, 8].fifth.should eq(5)
|
19
|
+
end
|
20
|
+
specify("#sixth") do
|
21
|
+
[1, 2, 3, 4, 5, 6, 7, 8].sixth.should eq(6)
|
22
|
+
end
|
23
|
+
specify("#seventh") do
|
24
|
+
[1, 2, 3, 4, 5, 6, 7, 8].seventh.should eq(7)
|
25
|
+
end
|
26
|
+
specify("#eighth") do
|
27
|
+
[1, 2, 3, 4, 5, 6, 7, 8].eighth.should eq(8)
|
28
|
+
end
|
29
|
+
specify("#array?") do
|
30
|
+
[1, 2, 3].array?.should be_true
|
31
|
+
end
|
32
|
+
specify("#avg") do
|
33
|
+
[1, 2, 3].avg.should eq(2)
|
34
|
+
end
|
35
|
+
specify("#clean") do
|
36
|
+
t = [1, nil]
|
37
|
+
t = t.clean(1)
|
38
|
+
t.should eq([nil])
|
39
|
+
end
|
40
|
+
specify("#clean!") do
|
41
|
+
t = [1, nil]
|
42
|
+
t.clean!(1)
|
43
|
+
t.should eq([nil])
|
44
|
+
end
|
45
|
+
specify("#complete_to") do
|
46
|
+
[1, 2, 3].complete_to(5, :a).should eq([1, 2, 3, :a, :a])
|
47
|
+
[1, 2, 3, 4, 5].complete_to(3).should eq([1, 2, 3])
|
48
|
+
end
|
49
|
+
specify("#complete_to!") do
|
50
|
+
t = [1, 2, 3]
|
51
|
+
t.complete_to! 5, :a
|
52
|
+
t.should eq([1, 2, 3, :a, :a])
|
53
|
+
|
54
|
+
t = [1, 2, 3, 4, 5]
|
55
|
+
t.complete_to! 3
|
56
|
+
t.should eq([1, 2, 3 ])
|
57
|
+
end
|
58
|
+
specify("#cut") do
|
59
|
+
[1, 2, 3, 4].cut(2).should eq([1, 2])
|
60
|
+
[1, 2, 3, 4].cut(4).should eq([])
|
61
|
+
[1, 2, 3, 4].cut(6).should eq([])
|
62
|
+
end
|
63
|
+
specify("#cut!") do
|
64
|
+
t = [1, 2, 3, 4]
|
65
|
+
t.cut! 2
|
66
|
+
t.should eq([1, 2])
|
67
|
+
|
68
|
+
t = [1, 2, 3, 4]
|
69
|
+
t.cut! 4
|
70
|
+
t.should eq([])
|
71
|
+
|
72
|
+
t = [1, 2, 3, 4]
|
73
|
+
t.cut! 6
|
74
|
+
t.should eq([])
|
75
|
+
end
|
76
|
+
specify("#drop!") do
|
77
|
+
t = [1, 2, 3, 4, 5]
|
78
|
+
t.drop! 2
|
79
|
+
t.should eq([3, 4, 5])
|
80
|
+
|
81
|
+
t = [1, 2, 3, 4, 5]
|
82
|
+
t.drop! 0
|
83
|
+
t.should eq([1, 2, 3, 4, 5])
|
84
|
+
|
85
|
+
t = [1, 2, 3, 4, 5]
|
86
|
+
expect { t.drop! -2 }.to raise_error(ArgumentError, 'attempt to drop negative size')
|
87
|
+
end
|
88
|
+
specify("#expand_by") do
|
89
|
+
[1, 2, 3].expand_by(:a, :b).should eq([1, :a, :b, 2, :a, :b, 3, :a, :b])
|
90
|
+
[1, 2, 3].expand_by([:a, :b]).should eq([1, [:a, :b], 2, [:a, :b], 3, [:a, :b]])
|
91
|
+
[].expand_by([:a, :b]).should eq([ ])
|
92
|
+
end
|
93
|
+
specify("#expand_by!") do
|
94
|
+
t = [1, 2, 3]
|
95
|
+
t.expand_by! :a, :b
|
96
|
+
t.should eq([1, :a, :b, 2, :a, :b, 3, :a, :b])
|
97
|
+
|
98
|
+
t = [1, 2, 3]
|
99
|
+
t.expand_by! [:a, :b]
|
100
|
+
t.should eq([1, [:a, :b], 2, [:a, :b], 3, [:a, :b]])
|
101
|
+
|
102
|
+
t = []
|
103
|
+
t.expand_by! [:a, :b]
|
104
|
+
t.should eq([ ])
|
105
|
+
end
|
106
|
+
specify("#interlace") do
|
107
|
+
[1, 2, 3].interlace(:a, :b).should eq([1, :a, :b, 2, :a, :b, 3])
|
108
|
+
end
|
109
|
+
specify("#interlace!") do
|
110
|
+
t = [1, 2, 3]
|
111
|
+
t.interlace! :a, :b
|
112
|
+
t.should eq([1, :a, :b, 2, :a, :b, 3])
|
113
|
+
end
|
114
|
+
specify("#merge!") do
|
115
|
+
t = [1, 2, 3]
|
116
|
+
t.merge!([:a, :b])
|
117
|
+
t.should eq([1, 2, 3, :a, :b])
|
118
|
+
end
|
119
|
+
specify("#remove!") do
|
120
|
+
t = [1, 2, 3, 4]
|
121
|
+
t.remove! { |i| i >= 3 }
|
122
|
+
t.should eq([1, 2])
|
123
|
+
|
124
|
+
t = [1, 2, 3, 4]
|
125
|
+
t.remove! { false }
|
126
|
+
t.should eq([1, 2, 3, 4])
|
127
|
+
|
128
|
+
t = [1, 2, 3, 4]
|
129
|
+
t.remove! { true }
|
130
|
+
t.should eq([ ])
|
131
|
+
end
|
132
|
+
specify("#shrink_to") do
|
133
|
+
[1, 2, 3, 4, 5].shrink_to(3).should eq([1, 2, 3])
|
134
|
+
[1, 2, 3, 4, 5].shrink_to(6).should eq([1, 2, 3, 4, 5])
|
135
|
+
[1, 2, 3, 4, 5].shrink_to(0).should eq([])
|
136
|
+
expect { [1, 2, 3, 4, 5].shrink_to(-3) }.to raise_error(ArgumentError, "negative argument")
|
137
|
+
end
|
138
|
+
specify("#shrink_to!") do
|
139
|
+
t = [1, 2, 3, 4, 5]
|
140
|
+
t.shrink_to! 3
|
141
|
+
t.should eq([1, 2, 3])
|
142
|
+
|
143
|
+
t = [1, 2, 3, 4, 5]
|
144
|
+
t.shrink_to! 6
|
145
|
+
t.should eq([1, 2, 3, 4, 5])
|
146
|
+
|
147
|
+
t = [1, 2, 3, 4, 5]
|
148
|
+
expect { t.shrink_to! -3 }.to raise_error(ArgumentError, "negative argument")
|
149
|
+
|
150
|
+
t = [1, 2, 3, 4, 5]
|
151
|
+
t.shrink_to! 0
|
152
|
+
t.should eq([])
|
153
|
+
end
|
154
|
+
specify("#sum") do
|
155
|
+
[1, 2, 3].sum.should eq(6)
|
156
|
+
end
|
157
|
+
specify("#to_h") do
|
158
|
+
[[:a, :b], [:b, :a]].to_h.should eq({:a => :b, :b => :a})
|
159
|
+
[:a, :b, :b, :a].to_h(:flat).should eq({:a => :b, :b => :a})
|
160
|
+
end
|
161
|
+
specify("#to_set") do
|
162
|
+
[1, 2, 3].to_set.should eq(Set::new([1, 2, 3]))
|
163
|
+
end
|
164
|
+
specify("#zip!") do
|
165
|
+
t = [1, 2, 3]
|
166
|
+
t.zip!
|
167
|
+
t.should eq([1, 2, 3].zip)
|
168
|
+
|
169
|
+
t = [1, 2, 3]
|
170
|
+
t.zip!([:a, :b], [:c], [:d, :e, :f])
|
171
|
+
t.should eq([1, 2, 3].zip([:a, :b], [:c], [:d, :e, :f]))
|
172
|
+
|
173
|
+
t = [1, 2, 3]
|
174
|
+
t.zip!([:a, :b], [:d, :e, :f], [:c])
|
175
|
+
t.should eq([1, 2, 3].zip([:a, :b], [:d, :e, :f], [:c]))
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
4
|
+
|
5
|
+
require "spec_helper"
|
6
|
+
|
7
|
+
describe "FalseClass" do
|
8
|
+
specify("#boolean?") do
|
9
|
+
false.boolean?.should be_true
|
10
|
+
end
|
11
|
+
specify("#convert") do
|
12
|
+
false.convert(:a, :b).should eq(:b)
|
13
|
+
end
|
14
|
+
specify("#false?") do
|
15
|
+
false.false?.should be_true
|
16
|
+
end
|
17
|
+
specify("#to_i") do
|
18
|
+
false.to_i.should eq(0)
|
19
|
+
end
|
20
|
+
specify("#true?") do
|
21
|
+
(not false.true?).should be_true
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
4
|
+
|
5
|
+
require "spec_helper"
|
6
|
+
|
7
|
+
describe "File" do
|
8
|
+
specify("#touch") do
|
9
|
+
File.touch("./~test1")
|
10
|
+
File.exists?("./~test1").should be_true
|
11
|
+
end
|
12
|
+
specify("#write") do
|
13
|
+
File.write("./~test2", "some string")
|
14
|
+
File.read("./~test2").should eq("some string")
|
15
|
+
end
|
16
|
+
|
17
|
+
after(:all) do
|
18
|
+
File.unlink("./~test1")
|
19
|
+
File.unlink("./~test2")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,223 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
4
|
+
|
5
|
+
require "spec_helper"
|
6
|
+
|
7
|
+
describe "Hash" do
|
8
|
+
specify("#all_pairs?") do
|
9
|
+
t = { :a => 1, :b => 2, :c => 3, :d => 4 }
|
10
|
+
t.all_pairs? { |k, v| k.in? [:a, :b] or v > 2 }.should be_true
|
11
|
+
t.all_pairs? { |k, v| v > 2 }.should be_false
|
12
|
+
{ }.all_pairs?.should be_true
|
13
|
+
end
|
14
|
+
specify("#asort") do
|
15
|
+
h = { 3 => :c, 1 => :a, 2 => :b }
|
16
|
+
h.asort.should eq({ 1 => :a, 2 => :b, 3 => :c })
|
17
|
+
end
|
18
|
+
specify("#asort!") do
|
19
|
+
h = { 3 => :c, 1 => :a, 2 => :b }
|
20
|
+
h.asort!
|
21
|
+
h.should eq({ 1 => :a, 2 => :b, 3 => :c })
|
22
|
+
end
|
23
|
+
specify("#avg") do
|
24
|
+
{ :a => 1, :b => 2 }.avg.should eq(1.5)
|
25
|
+
end
|
26
|
+
specify("#clean") do
|
27
|
+
t = { :a => 1, :b => nil }
|
28
|
+
t = t.clean(1)
|
29
|
+
t.should eq({ :b => nil })
|
30
|
+
end
|
31
|
+
specify("#clean!") do
|
32
|
+
t = { :a => 1, :b => nil }
|
33
|
+
t.clean!(1)
|
34
|
+
t.should eq({ :b => nil })
|
35
|
+
end
|
36
|
+
specify("::combine") do
|
37
|
+
keys = [:a, :b, :c]
|
38
|
+
values = [1, 2, 3]
|
39
|
+
|
40
|
+
Hash::combine(keys, values).should eq({ :a => 1, :b => 2, :c => 3 })
|
41
|
+
Hash::combine([ ], [ ]).should eq({ })
|
42
|
+
Hash::combine([:a, :b], [ ]).should eq({ :a => nil, :b => nil })
|
43
|
+
Hash::combine([ ], [1, 2]).should eq({ })
|
44
|
+
end
|
45
|
+
specify("#compact") do
|
46
|
+
t = { :a => 1, :b => nil }
|
47
|
+
t = t.compact
|
48
|
+
t.should eq({ :a => 1 })
|
49
|
+
end
|
50
|
+
specify("#compact!") do
|
51
|
+
t = { :a => 1, :b => nil }
|
52
|
+
t.compact!
|
53
|
+
t.should eq({ :a => 1 })
|
54
|
+
end
|
55
|
+
specify("::create") do
|
56
|
+
proc = Proc::new { |dict, key| dict[key] = "" }
|
57
|
+
t1 = Hash::create(:default)
|
58
|
+
t2 = Hash::define(&proc)
|
59
|
+
|
60
|
+
t1.default.should eq(:default)
|
61
|
+
t2.default_proc.should eq(proc)
|
62
|
+
end
|
63
|
+
specify("::define") do
|
64
|
+
proc = Proc::new { |dict, key| dict[key] = "" }
|
65
|
+
t1 = Hash::define({:a => :b}, :default)
|
66
|
+
t2 = Hash::define({:a => :b}, &proc)
|
67
|
+
|
68
|
+
t1.should eq(:a => :b)
|
69
|
+
t1.default.should eq(:default)
|
70
|
+
t2.default_proc.should eq(proc)
|
71
|
+
end
|
72
|
+
specify("#flip") do
|
73
|
+
t = { :a => 1, :b => 2, :c => 2 }
|
74
|
+
result = t.flip
|
75
|
+
(result == { 1 => :a, 2 => :c } or result == { 1 => :a, 2 => :b }).should be_true
|
76
|
+
end
|
77
|
+
specify("#flip!") do
|
78
|
+
t = { :a => 1, :b => 2, :c => 2 }
|
79
|
+
t.flip!
|
80
|
+
(t == { 1 => :a, 2 => :c } or t == { 1 => :a, 2 => :b }).should be_true
|
81
|
+
end
|
82
|
+
specify("#get_pairs") do
|
83
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
84
|
+
result = [ ]
|
85
|
+
h.get_pairs(:a, :c, :d) { |i| result << i }
|
86
|
+
result.should eq([[:a, 1], [:c, 3]])
|
87
|
+
end
|
88
|
+
specify("#get_items") do
|
89
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
90
|
+
h.get_items(:a, :c, :d).should eq({ :a => 1, :c => 3 })
|
91
|
+
end
|
92
|
+
specify("#get_values") do
|
93
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
94
|
+
h.get_values(:a, :c, :d).should eq([1, 3])
|
95
|
+
end
|
96
|
+
specify("#get_values") do
|
97
|
+
{ :a => 1, :b => 2, :c => 3 }.hash?.should be_true
|
98
|
+
end
|
99
|
+
specify("#has_all?") do
|
100
|
+
{ :a => 1, :b => 2, :c => 3 }.has_all?([:b, :c]).should be_true
|
101
|
+
{ :a => 1, :b => 2, :c => 3 }.has_all?([:b, :d]).should be_false
|
102
|
+
end
|
103
|
+
specify("#has_some?") do
|
104
|
+
{ :a => 1, :b => 2, :c => 3 }.has_some?([:b, :c]).should be_true
|
105
|
+
{ :a => 1, :b => 2, :c => 3 }.has_some?([:b, :d]).should be_true
|
106
|
+
{ :a => 1, :b => 2, :c => 3 }.has_some?([:d, :e]).should be_false
|
107
|
+
end
|
108
|
+
specify("#map_values") do
|
109
|
+
{ :a => 1, :b => 2 }.map_values { |i| i + 1 }.should eq({ :a => 2, :b => 3 })
|
110
|
+
end
|
111
|
+
specify("#map_values!") do
|
112
|
+
t = { :a => 1, :b => 2 }
|
113
|
+
t.map_values! { |i| i + 1 }
|
114
|
+
t.should eq({ :a => 2, :b => 3 })
|
115
|
+
end
|
116
|
+
specify("#deep_merge") do
|
117
|
+
h1 = {:a => {:b => :c, :d => :e}}
|
118
|
+
h2 = {:a => {:d => :f, :h => :i}}
|
119
|
+
h = h1.deep_merge(h2)
|
120
|
+
h.should eq({:a => {:b => :c, :d => :f, :h => :i}})
|
121
|
+
h1.should eq({:a => {:b => :c, :d => :e}})
|
122
|
+
end
|
123
|
+
specify("#deep_merge!") do
|
124
|
+
h = {:a => {:b => :c, :d => :e}}
|
125
|
+
h.deep_merge!({:a => {:d => :f, :h => :i}})
|
126
|
+
h.should eq({:a => {:b => :c, :d => :f, :h => :i}})
|
127
|
+
end
|
128
|
+
specify("#keys_to_sym") do
|
129
|
+
h = {"a" => "b", 2 => "c", "d" => "e"}
|
130
|
+
h.keys_to_sym.should eq({:a => "b", 2 => "c", :d => "e"})
|
131
|
+
end
|
132
|
+
specify("#keys_to_sym!") do
|
133
|
+
h = {"a" => "b", 2 => "c", "d" => "e"}
|
134
|
+
h.keys_to_sym!
|
135
|
+
h.should eq({:a => "b", 2 => "c", :d => "e"})
|
136
|
+
end
|
137
|
+
specify("#ksort") do
|
138
|
+
h = { 3 => :b, 1 => :a, 2 => :c }
|
139
|
+
h.ksort.should eq({ 1 => :a, 2 => :c, 3 => :b })
|
140
|
+
end
|
141
|
+
specify("#ksort!") do
|
142
|
+
h = { 3 => :b, 1 => :a, 2 => :c }
|
143
|
+
h.ksort!
|
144
|
+
h.should eq({ 1 => :a, 2 => :c, 3 => :b })
|
145
|
+
end
|
146
|
+
specify("#map_keys!") do
|
147
|
+
h = {"a" => 1, "b" => 2}
|
148
|
+
h.map_keys! do |k|
|
149
|
+
k.to_sym
|
150
|
+
end
|
151
|
+
|
152
|
+
h.should eq({:a => 1, :b => 2})
|
153
|
+
end
|
154
|
+
specify("#recreate!") do
|
155
|
+
h = Hash::create(:default, {:a => 1, :b => 2})
|
156
|
+
h.recreate!
|
157
|
+
h.should eq({ })
|
158
|
+
h.default.should eq(:default)
|
159
|
+
|
160
|
+
proc = Proc::new { }
|
161
|
+
h = Hash::create(nil, {:a => 1, :b => 2}, &proc)
|
162
|
+
h.recreate!
|
163
|
+
h.default_proc.should eq(proc)
|
164
|
+
end
|
165
|
+
specify("#remove!") do
|
166
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 4}
|
167
|
+
h.remove! { |k, v| k == :c or v > 2 }.should eq({:c => 3, :d => 4})
|
168
|
+
h.should eq({:a => 1, :b => 2})
|
169
|
+
end
|
170
|
+
specify("#reverse") do
|
171
|
+
h = {:a => 1, :b => 2, :c => 3}
|
172
|
+
h.reverse.should eq({:c => 3, :b => 2, :a => 1})
|
173
|
+
{ }.reverse.should eq({ })
|
174
|
+
end
|
175
|
+
specify("#reverse!") do
|
176
|
+
h = {:a => 1, :b => 2, :c => 3}
|
177
|
+
h.reverse!
|
178
|
+
h.should eq({:c => 3, :b => 2, :a => 1})
|
179
|
+
|
180
|
+
h = { }
|
181
|
+
h.reverse!
|
182
|
+
h.should eq({ })
|
183
|
+
end
|
184
|
+
specify("#some?") do
|
185
|
+
h = {:a => 1, :b => 2, :c => 3}
|
186
|
+
h.some? { |v| v > 1 }.should be_true
|
187
|
+
h.some? { |v| v < 0 }.should be_false
|
188
|
+
{ }.some? { |v| b < 0 }.should be_false
|
189
|
+
end
|
190
|
+
specify("#some_pairs?") do
|
191
|
+
h = {:a => 1, :b => 2, :c => 3}
|
192
|
+
h.some_pairs? { |k, v| v > 1 }.should be_true
|
193
|
+
h.some_pairs? { |k, v| k == :a }.should be_true
|
194
|
+
h.some_pairs? { |k, v| k == :d }.should be_false
|
195
|
+
h.some_pairs? { |k, v| v > 4 }.should be_false
|
196
|
+
{ }.some_pairs? { |k, v| b < 0 }.should be_false
|
197
|
+
end
|
198
|
+
specify("#sort!") do
|
199
|
+
h = { 3 => :b, 1 => :a, 2 => :c }
|
200
|
+
h.sort!
|
201
|
+
h.should eq({ 1 => :a, 2 => :c, 3 => :b })
|
202
|
+
end
|
203
|
+
specify("#sum") do
|
204
|
+
{ :a => 1, :b => 2 }.sum.should eq(3)
|
205
|
+
{ :a => "a", :b => "b" }.sum.should eq("ab")
|
206
|
+
{ }.sum.should be_nil
|
207
|
+
end
|
208
|
+
specify("#take_pairs") do
|
209
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
210
|
+
result = [ ]
|
211
|
+
h.take_pairs(:a, :c, :d) { |i| result << i }
|
212
|
+
result.should eq([[:a, 1], [:c, 3], [:d, nil]])
|
213
|
+
end
|
214
|
+
specify("#take_items") do
|
215
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
216
|
+
h.take_items(:a, :c, :d).should eq({ :a => 1, :c => 3, :d => nil })
|
217
|
+
end
|
218
|
+
specify("#take_values") do
|
219
|
+
h = { :a => 1, :b => 2, :c => 3 }
|
220
|
+
h.take_values(:a, :c, :d).should eq([1, 3, nil])
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|