facets 2.5.1 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +113 -13
- data/MANIFEST +23 -19
- data/RELEASE +22 -22
- data/VERSION +1 -1
- data/doc/{archive → changelogs}/CHANGELOG-0.10.30 +0 -0
- data/doc/{archive → changelogs}/CHANGELOG-1.8.54 +0 -0
- data/doc/{archive → changelogs}/CHANGELOG-2.4.5 +0 -0
- data/doc/log/basic_stats/index.html +4 -4
- data/doc/rdoc/core/classes/Array.html +148 -148
- data/doc/rdoc/core/classes/Binding.html +45 -45
- data/doc/rdoc/core/classes/Class.html +38 -38
- data/doc/rdoc/core/classes/Comparable.html +26 -26
- data/doc/rdoc/core/classes/Enumerable.html +224 -139
- data/doc/rdoc/core/classes/Enumerable/Enumerator.html +126 -13
- data/doc/rdoc/core/classes/Enumerator.html +123 -13
- data/doc/rdoc/core/classes/Exception.html +8 -8
- data/doc/rdoc/core/classes/File.html +58 -58
- data/doc/rdoc/core/classes/FileTest.html +4 -4
- data/doc/rdoc/core/classes/Float.html +8 -8
- data/doc/rdoc/core/classes/Functor.html +12 -12
- data/doc/rdoc/core/classes/Hash.html +289 -266
- data/doc/rdoc/core/classes/Indexable.html +110 -110
- data/doc/rdoc/core/classes/Integer.html +2 -2
- data/doc/rdoc/core/classes/Kernel.html +329 -329
- data/doc/rdoc/core/classes/Lazy/Promise.html +1 -1
- data/doc/rdoc/core/classes/MatchData.html +15 -15
- data/doc/rdoc/core/classes/Module.html +161 -161
- data/doc/rdoc/core/classes/NilClass.html +23 -23
- data/doc/rdoc/core/classes/Numeric.html +27 -27
- data/doc/rdoc/core/classes/Object.html +8 -8
- data/doc/rdoc/core/classes/ObjectSpace.html +5 -5
- data/doc/rdoc/core/classes/Proc.html +22 -22
- data/doc/rdoc/core/classes/Regexp.html +1 -1
- data/doc/rdoc/core/classes/Stackable.html +31 -31
- data/doc/rdoc/core/classes/String.html +270 -270
- data/doc/rdoc/core/classes/Symbol.html +1 -1
- data/doc/rdoc/core/classes/TrueClass.html +8 -8
- data/doc/rdoc/core/classes/UnboundMethod.html +12 -12
- data/doc/rdoc/core/created.rid +1 -1
- data/doc/rdoc/core/files/lib/core/facets/hash/dearray_values_rb.html +92 -0
- data/doc/rdoc/core/files/lib/core/facets/to_hash_rb.html +2 -1
- data/doc/rdoc/core/fr_file_index.html +1 -0
- data/doc/rdoc/core/fr_method_index.html +401 -385
- data/doc/{archive → release-notes}/RELEASE-2.0.5 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.1.0 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.1.1 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.1.2 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.1.3 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.2.0 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.2.1 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.3.0 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.0 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.1 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.2 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.3 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.4 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.4.5 +0 -0
- data/doc/{archive → release-notes}/RELEASE-2.5.0 +0 -0
- data/lib/core/facets/hash/dearray_values.rb +46 -0
- data/lib/core/facets/to_hash.rb +109 -74
- data/task/setup.rake +17 -9
- data/test/core/hash/test_dearray_values.rb +19 -0
- data/test/core/test_to_hash.rb +116 -24
- metadata +26 -21
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Any array values with be replaced with the first element of the array.
|
4
|
+
# Arrays with no elements will be set to nil.
|
5
|
+
#
|
6
|
+
# h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
7
|
+
# h.dearray_values #=> { :a=>1, :b=>1, :c=>3, :d=>nil }
|
8
|
+
#
|
9
|
+
# CREDIT: Trans
|
10
|
+
|
11
|
+
def dearray_values(index=0)
|
12
|
+
h = {}
|
13
|
+
each do |k,v|
|
14
|
+
case v
|
15
|
+
when Array
|
16
|
+
h[k] = v[index] || v[-1]
|
17
|
+
else
|
18
|
+
h[k] = v
|
19
|
+
end
|
20
|
+
end
|
21
|
+
h
|
22
|
+
end
|
23
|
+
|
24
|
+
# Any array values with one or no elements will be set to the element
|
25
|
+
# or nil.
|
26
|
+
#
|
27
|
+
# h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
28
|
+
# h.dearray_singular_values #=> { :a=>1, :b=>[1,2], :c=>3, :d=>nil }
|
29
|
+
#
|
30
|
+
# CREDIT: Trans
|
31
|
+
|
32
|
+
def dearray_singular_values
|
33
|
+
h = {}
|
34
|
+
each do |k,v|
|
35
|
+
case v
|
36
|
+
when Array
|
37
|
+
h[k] = (v.size < 2) ? v[0] : v
|
38
|
+
else
|
39
|
+
h[k] = v
|
40
|
+
end
|
41
|
+
end
|
42
|
+
h
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
data/lib/core/facets/to_hash.rb
CHANGED
@@ -1,51 +1,6 @@
|
|
1
1
|
require 'enumerator' if RUBY_VERSION < "1.9"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
# Any array values with less one or no elements will have the element
|
6
|
-
# or nil set as the value instead.
|
7
|
-
#
|
8
|
-
# h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
9
|
-
# h.dearray_values #=> { :a=>1, :b=>1, :c=>3, :d=>nil }
|
10
|
-
#
|
11
|
-
# CREDIT: Trans
|
12
|
-
|
13
|
-
def dearray_values(index=0)
|
14
|
-
h = {}
|
15
|
-
each do |k,v|
|
16
|
-
case v
|
17
|
-
when Array
|
18
|
-
h[k] = v[index] || v[-1]
|
19
|
-
else
|
20
|
-
h[k] = v
|
21
|
-
end
|
22
|
-
end
|
23
|
-
h
|
24
|
-
end
|
25
|
-
|
26
|
-
# Any array values with less one or no elements will have the element
|
27
|
-
# or nil set as the value instead.
|
28
|
-
#
|
29
|
-
# h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
30
|
-
# h.dearray_singluar_values #=> { :a=>1, :b=>[1,2], :c=>3, :d=>nil }
|
31
|
-
#
|
32
|
-
# CREDIT: Trans
|
33
|
-
|
34
|
-
def dearray_singluar_values
|
35
|
-
h = {}
|
36
|
-
each do |k,v|
|
37
|
-
case v
|
38
|
-
when Array
|
39
|
-
h[k] = (v.size < 2) ? v[0] : v
|
40
|
-
else
|
41
|
-
h[k] = v
|
42
|
-
end
|
43
|
-
end
|
44
|
-
h
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
3
|
+
require 'facets/hash/dearray_values'
|
49
4
|
|
50
5
|
class Array
|
51
6
|
|
@@ -193,7 +148,7 @@ class Array
|
|
193
148
|
# a = [ [:a,1,2], [:b,2], [:c], :d ]
|
194
149
|
# a.to_h #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
195
150
|
#
|
196
|
-
# If the
|
151
|
+
# If the first entry of any subelements are the same, then
|
197
152
|
# the value will be set to the last occuring value.
|
198
153
|
#
|
199
154
|
# a = [ :x, [:x], [:x,1,2], [:x,3], [:x,4] ]
|
@@ -213,11 +168,11 @@ class Array
|
|
213
168
|
# a = [ [:a,1,2], [:b,2], [:c], :d ]
|
214
169
|
# a.to_h #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
215
170
|
#
|
216
|
-
# If the
|
171
|
+
# If the first entry of the subelements is the same, then
|
217
172
|
# the values will be merged using #concat.
|
218
173
|
#
|
219
174
|
# a = [ [:a,1,2], [:a,3], [:a,4], [:a], :a ]
|
220
|
-
# a.to_h_multi #=> { :a=>[1,2,3,4
|
175
|
+
# a.to_h_multi #=> { :a=>[1,2,3,4] }
|
221
176
|
#
|
222
177
|
def to_h_multi
|
223
178
|
h = {}
|
@@ -253,6 +208,12 @@ class Hash
|
|
253
208
|
|
254
209
|
def to_h; rehash; end
|
255
210
|
|
211
|
+
# Returns _self_.
|
212
|
+
#
|
213
|
+
# CREDIT: Trans
|
214
|
+
|
215
|
+
def to_hash; self; end
|
216
|
+
|
256
217
|
end
|
257
218
|
|
258
219
|
module Enumerable
|
@@ -266,6 +227,26 @@ module Enumerable
|
|
266
227
|
to_a.to_h(mode)
|
267
228
|
end
|
268
229
|
|
230
|
+
def to_h_auto
|
231
|
+
to_a.to_h_auto
|
232
|
+
end
|
233
|
+
|
234
|
+
def to_h_splat
|
235
|
+
to_a.to_h_splat
|
236
|
+
end
|
237
|
+
|
238
|
+
def to_h_flat
|
239
|
+
to_a.to_h_flat
|
240
|
+
end
|
241
|
+
|
242
|
+
def to_h_assoc
|
243
|
+
to_a.to_h_assoc
|
244
|
+
end
|
245
|
+
|
246
|
+
def to_h_multi
|
247
|
+
to_a.to_h_multi
|
248
|
+
end
|
249
|
+
|
269
250
|
#def to_hash
|
270
251
|
# to_a.to_hash
|
271
252
|
#end
|
@@ -290,37 +271,71 @@ if RUBY_VERSION < "1.9"
|
|
290
271
|
|
291
272
|
class Enumerable::Enumerator
|
292
273
|
|
293
|
-
# Convert an Enumerable::Enumerator object
|
274
|
+
# Convert an Enumerable::Enumerator object into a hash.
|
275
|
+
# This is equivalent to Array#to_h.
|
276
|
+
#
|
277
|
+
# e1 = [[1,:a],[2,:b],[3,:c]].to_enum
|
278
|
+
# e1.to_h #=> { 1=>:a, 2=>:b, 3=>:c }
|
294
279
|
#
|
295
|
-
#
|
296
|
-
#
|
297
|
-
#
|
298
|
-
#
|
299
|
-
# e3
|
300
|
-
# e3.to_h #=> { 1=>:a, 2=>:b, 3=>:c }
|
280
|
+
# e2 = [1,2,3,4,5].to_enum
|
281
|
+
# e2.to_h #=> {5=>nil, 1=>2, 3=>4}
|
282
|
+
#
|
283
|
+
# e3 = [1,2,1,3,1,5].to_enum
|
284
|
+
# e3.to_h #=> {1=>5}
|
301
285
|
#
|
302
286
|
# CREDIT: Sandor Szücs
|
303
287
|
|
304
288
|
def to_h(mode=nil)
|
305
289
|
to_a.to_h(mode)
|
306
290
|
end
|
291
|
+
|
292
|
+
# This is equivalent to Array#to_h_auto.
|
293
|
+
#
|
294
|
+
def to_h_auto
|
295
|
+
to_a.to_h_auto
|
296
|
+
end
|
297
|
+
|
298
|
+
# This is equivalent to Array#to_h_splat.
|
299
|
+
#
|
300
|
+
def to_h_splat
|
301
|
+
to_a.to_h_splat
|
302
|
+
end
|
303
|
+
|
304
|
+
# This is equivalent to Array#to_h_flat.
|
305
|
+
#
|
306
|
+
def to_h_flat
|
307
|
+
to_a.to_h_flat
|
308
|
+
end
|
309
|
+
|
310
|
+
# This is equivalent to Array#to_h_assoc.
|
311
|
+
#
|
312
|
+
def to_h_assoc
|
313
|
+
to_a.to_h_assoc
|
314
|
+
end
|
315
|
+
|
316
|
+
# This is equivalent to Array#to_h_multi.
|
317
|
+
#
|
318
|
+
def to_h_multi
|
319
|
+
to_a.to_h_multi
|
320
|
+
end
|
321
|
+
|
307
322
|
end
|
308
323
|
|
309
324
|
else
|
310
325
|
|
311
326
|
class Enumerator
|
312
327
|
|
313
|
-
# Convert an Enumerator object
|
314
|
-
#
|
315
|
-
# e3 = [[1,:a],[2,:b],[3,:c]].to_enum
|
316
|
-
# e3.to_h #=> { 1=>:a, 2=>:b, 3=>:c }
|
328
|
+
# Convert an Enumerator object into a hash.
|
329
|
+
# This is equivalent to Array#to_h.
|
317
330
|
#
|
318
|
-
# e1 = [1,2,3
|
319
|
-
# e1.to_h
|
331
|
+
# e1 = [[1,:a],[2,:b],[3,:c]].to_enum
|
332
|
+
# e1.to_h #=> { 1=>:a, 2=>:b, 3=>:c }
|
320
333
|
#
|
321
|
-
# e2 = [1,2,
|
322
|
-
# e2.to_h
|
334
|
+
# e2 = [1,2,3,4,5].to_enum
|
335
|
+
# e2.to_h #=> {5=>nil, 1=>2, 3=>4}
|
323
336
|
#
|
337
|
+
# e3 = [1,2,1,3,1,5].to_enum
|
338
|
+
# e3.to_h #=> {1=>5}
|
324
339
|
#
|
325
340
|
# CREDIT: Sandor Szücs
|
326
341
|
|
@@ -328,16 +343,36 @@ else
|
|
328
343
|
to_a.to_h(mode)
|
329
344
|
end
|
330
345
|
|
331
|
-
#
|
332
|
-
#
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
#
|
338
|
-
#
|
339
|
-
|
340
|
-
|
346
|
+
# This is equivalent to Array#to_h_auto.
|
347
|
+
#
|
348
|
+
def to_h_auto
|
349
|
+
to_a.to_h_auto
|
350
|
+
end
|
351
|
+
|
352
|
+
# This is equivalent to Array#to_h_splat.
|
353
|
+
#
|
354
|
+
def to_h_splat
|
355
|
+
to_a.to_h_splat
|
356
|
+
end
|
357
|
+
|
358
|
+
# This is equivalent to Array#to_h_flat.
|
359
|
+
#
|
360
|
+
def to_h_flat
|
361
|
+
to_a.to_h_flat
|
362
|
+
end
|
363
|
+
|
364
|
+
# This is equivalent to Array#to_h_assoc.
|
365
|
+
#
|
366
|
+
def to_h_assoc
|
367
|
+
to_a.to_h_assoc
|
368
|
+
end
|
369
|
+
|
370
|
+
# This is equivalent to Array#to_h_multi.
|
371
|
+
#
|
372
|
+
def to_h_multi
|
373
|
+
to_a.to_h_multi
|
374
|
+
end
|
375
|
+
|
341
376
|
end
|
342
377
|
|
343
378
|
end
|
data/task/setup.rake
CHANGED
@@ -1,13 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
|
2
|
+
def load_setup
|
3
|
+
begin
|
4
|
+
require 'setup'
|
5
|
+
$setup_installed = true
|
6
|
+
rescue LoadError
|
7
|
+
$setup_installed = false
|
8
|
+
puts "NOTP"
|
9
|
+
end
|
7
10
|
end
|
8
11
|
|
12
|
+
private :load_setup
|
13
|
+
|
9
14
|
desc "install to ruby site location"
|
10
|
-
task
|
15
|
+
task :install do
|
16
|
+
load_setup
|
17
|
+
|
11
18
|
if $setup_installed
|
12
19
|
sh "setup.rb all"
|
13
20
|
else
|
@@ -18,7 +25,9 @@ task "setup:install" do
|
|
18
25
|
end
|
19
26
|
|
20
27
|
desc "uninstall from ruby site location"
|
21
|
-
task
|
28
|
+
task :uninstall do
|
29
|
+
load_setup
|
30
|
+
|
22
31
|
if $setup_installed
|
23
32
|
sh "setup.rb uninstall"
|
24
33
|
else
|
@@ -27,4 +36,3 @@ task "setup:uninstall" do
|
|
27
36
|
puts "or 'gem install setup'."
|
28
37
|
end
|
29
38
|
end
|
30
|
-
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'facets/hash/dearray_values'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestHashDearrayValues < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_dearray_values
|
7
|
+
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
8
|
+
x = { :a=>1, :b=>1, :c=>3, :d=>nil }
|
9
|
+
assert_equal(x, h.dearray_values)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_dearray_singular_values
|
13
|
+
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
14
|
+
x = { :a=>1, :b=>[1,2], :c=>3, :d=>nil }
|
15
|
+
assert_equal(x, h.dearray_singular_values )
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
data/test/core/test_to_hash.rb
CHANGED
@@ -14,6 +14,21 @@ class TestArrayConversion < Test::Unit::TestCase
|
|
14
14
|
# assert_raise(ArgumentError) { [[1,:a],:b].to_h }
|
15
15
|
#end
|
16
16
|
|
17
|
+
def test_to_h_auto
|
18
|
+
a0 = [ [:a,1], [:b,2] ]
|
19
|
+
x0 = { :a=>1, :b=>2 }
|
20
|
+
|
21
|
+
a1 = [ [:a,1,2], [:b,2], [:c], [:d] ]
|
22
|
+
x1 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
23
|
+
|
24
|
+
a2 = [ [:a,1,2], 2, :b, [:c,3], 9 ]
|
25
|
+
x2 = { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
26
|
+
|
27
|
+
assert_equal(x0, a0.to_h_auto)
|
28
|
+
assert_equal(x1, a1.to_h_auto)
|
29
|
+
assert_equal(x2, a2.to_h_auto)
|
30
|
+
end
|
31
|
+
|
17
32
|
def test_to_h_flat
|
18
33
|
a = [[1,2],3,4,5]
|
19
34
|
x = {1=>2,3=>4,5=>nil}
|
@@ -27,15 +42,33 @@ class TestArrayConversion < Test::Unit::TestCase
|
|
27
42
|
end
|
28
43
|
|
29
44
|
def test_to_h_assoc
|
30
|
-
|
31
|
-
|
32
|
-
|
45
|
+
a0 = [[:a,1],[:b,2],[:c],:d,[:a,5]]
|
46
|
+
x0 = {:a=>[5],:b=>[2],:c=>[],:d=>[]}
|
47
|
+
|
48
|
+
a1 = [ [:a,1,2], [:b,2], [:c], :d ]
|
49
|
+
x1 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
50
|
+
|
51
|
+
a2 = [ :x, [:x], [:x,1,2], [:x,3], [:x,4] ]
|
52
|
+
x2 = { :x=>[4] }
|
53
|
+
|
54
|
+
assert_equal(x0, a0.to_h_assoc)
|
55
|
+
assert_equal(x1, a1.to_h_assoc)
|
56
|
+
assert_equal(x2, a2.to_h_assoc)
|
33
57
|
end
|
34
58
|
|
35
59
|
def test_to_h_multi
|
36
|
-
|
37
|
-
|
38
|
-
|
60
|
+
a0 = [[:a,1],[:b,2],[:c],:d,[:a,5]]
|
61
|
+
x0 = {:a=>[1,5],:b=>[2],:c=>[],:d=>[]}
|
62
|
+
|
63
|
+
a1 = [ [:a,1,2], [:b,2], [:c], :d ]
|
64
|
+
x1 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
65
|
+
|
66
|
+
a2 = [ [:a,1,2], [:a,3], [:a,4], [:a], :a ]
|
67
|
+
x2 = { :a=>[1,2,3,4] }
|
68
|
+
|
69
|
+
assert_equal(x0, a0.to_h_multi)
|
70
|
+
assert_equal(x1, a1.to_h_multi)
|
71
|
+
assert_equal(x2, a2.to_h_multi)
|
39
72
|
end
|
40
73
|
|
41
74
|
#def test_to_h_assoc_arrayed
|
@@ -113,18 +146,6 @@ class TestHashConversion < Test::Unit::TestCase
|
|
113
146
|
assert_equal( a, a.to_h )
|
114
147
|
end
|
115
148
|
|
116
|
-
def test_dearray_values
|
117
|
-
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
118
|
-
x = { :a=>1, :b=>1, :c=>3, :d=>nil }
|
119
|
-
assert_equal(x, h.dearray_values)
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_dearray_singluar_values
|
123
|
-
h = { :a=>[1], :b=>[1,2], :c=>3, :d=>[] }
|
124
|
-
x = { :a=>1, :b=>[1,2], :c=>3, :d=>nil }
|
125
|
-
assert_equal(x, h.dearray_singluar_values )
|
126
|
-
end
|
127
|
-
|
128
149
|
end
|
129
150
|
|
130
151
|
|
@@ -138,15 +159,86 @@ end
|
|
138
159
|
|
139
160
|
|
140
161
|
class TestEnumeratorConversion < Test::Unit::TestCase
|
141
|
-
|
162
|
+
|
142
163
|
def test_to_h
|
164
|
+
e0 = [1,2,3,4].to_enum
|
165
|
+
x0 = {1=>2,3=>4}
|
143
166
|
e1 = [[1,:a],[2,:b],[3,:c]].to_enum
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
assert_equal(
|
148
|
-
assert_equal(
|
167
|
+
x1 = { 1=>:a, 2=>:b, 3=>:c }
|
168
|
+
e2 = [:a,1,:b].to_enum
|
169
|
+
x2 = {:a=>1,:b=>nil}
|
170
|
+
assert_equal(x0, e0.to_h)
|
171
|
+
assert_equal(x1, e1.to_h)
|
172
|
+
assert_equal(x2, e2.to_h)
|
149
173
|
end
|
150
174
|
|
175
|
+
def test_to_h_auto
|
176
|
+
e0 = [ [:a,1], [:b,2] ].to_enum
|
177
|
+
x0 = { :a=>1, :b=>2 }
|
178
|
+
e1 = [ [:a,1,2], [:b,2], [:c], [:d] ].to_enum
|
179
|
+
x1 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
180
|
+
e2 = [ [:a,1,2], 2, :b, [:c,3], 9 ].to_enum
|
181
|
+
x2 = { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
|
182
|
+
assert_equal(x0, e0.to_h_auto)
|
183
|
+
assert_equal(x1, e1.to_h_auto)
|
184
|
+
assert_equal(x2, e2.to_h_auto)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_to_h_flat
|
188
|
+
e1 = [[1,2],3,4,5].to_enum
|
189
|
+
x1 = {1=>2,3=>4,5=>nil}
|
190
|
+
e2 = [:a,1,[:b,2,:c]].to_enum
|
191
|
+
x2 = { :a=>1, :b=>2, :c=>nil }
|
192
|
+
e3 = [[:a,1],[:b,2]].to_enum
|
193
|
+
x3 = { :a=>1, :b=>2 }
|
194
|
+
assert_equal(x1, e1.to_h_flat)
|
195
|
+
assert_equal(x1, e1.to_h(:flat))
|
196
|
+
assert_equal(x2, e2.to_h_flat)
|
197
|
+
assert_equal(x2, e2.to_h(:flat))
|
198
|
+
assert_equal(x3, e3.to_h_flat)
|
199
|
+
assert_equal(x3, e3.to_h(:flat))
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_to_h_splat
|
203
|
+
e1 = [1,2,3,4,[5,6]].to_enum
|
204
|
+
x1 = { 1=>2, 3=>4, [5,6]=>nil }
|
205
|
+
e2 = [:a,1,:b,2,:c].to_enum
|
206
|
+
x2 = { :a=>1, :b=>2, :c=>nil }
|
207
|
+
assert_equal(x1, e1.to_h_splat)
|
208
|
+
assert_equal(x1, e1.to_h(:splat))
|
209
|
+
assert_equal(x2, e2.to_h_splat)
|
210
|
+
assert_equal(x2, e2.to_h(:splat))
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_to_h_assoc
|
214
|
+
e1 = [[:a,1],[:b,2],[:c],:d,[:a,5]].to_enum
|
215
|
+
x1 = {:a=>[5],:b=>[2],:c=>[],:d=>[]}
|
216
|
+
e2 = [ [:a,1,2], [:b,2], [:c], :d ].to_enum
|
217
|
+
x2 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
218
|
+
e3 = [ :x, [:x], [:x,1,2], [:x,3], [:x,4] ].to_enum
|
219
|
+
x3 = { :x=>[4] }
|
220
|
+
assert_equal(x1, e1.to_h_assoc)
|
221
|
+
assert_equal(x1, e1.to_h(:assoc))
|
222
|
+
assert_equal(x2, e2.to_h_assoc)
|
223
|
+
assert_equal(x2, e2.to_h(:assoc))
|
224
|
+
assert_equal(x3, e3.to_h_assoc)
|
225
|
+
assert_equal(x3, e3.to_h(:assoc))
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_to_h_multi
|
229
|
+
e1 = [[:a,1],[:b,2],[:c],:d,[:a,5]].to_enum
|
230
|
+
x1 = {:a=>[1,5],:b=>[2],:c=>[],:d=>[]}
|
231
|
+
e2 = [ [:a,1,2], [:b,2], [:c], :d ]
|
232
|
+
x2 = { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
|
233
|
+
e3 = [ [:a,1,2], [:a,3], [:a,4], [:a], :a ]
|
234
|
+
x3 = { :a=>[1,2,3,4] }
|
235
|
+
assert_equal(x1, e1.to_h_multi)
|
236
|
+
assert_equal(x1, e1.to_h(:multi))
|
237
|
+
assert_equal(x2, e2.to_h_multi)
|
238
|
+
assert_equal(x2, e2.to_h(:multi))
|
239
|
+
assert_equal(x3, e3.to_h_multi)
|
240
|
+
assert_equal(x3, e3.to_h(:multi))
|
241
|
+
end
|
242
|
+
|
151
243
|
end
|
152
244
|
|