equal_rights_for_hash 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/equal_rights_for_hash.gemspec +1 -1
- data/lib/equal_rights_for_hash.rb +1 -3
- data/test/to_h_test.rb +12 -0
- metadata +1 -1
@@ -8,9 +8,7 @@ class Object
|
|
8
8
|
def to_h
|
9
9
|
if respond_to? :to_hash
|
10
10
|
to_hash
|
11
|
-
elsif respond_to? :to_ary and not all? {|item|
|
12
|
-
item.respond_to? :to_ary and item.size <= 2 }
|
13
|
-
then
|
11
|
+
elsif respond_to? :to_ary and not all? {|item| item.respond_to? :to_ary }
|
14
12
|
Hash[*self]
|
15
13
|
else
|
16
14
|
Hash[self]
|
data/test/to_h_test.rb
CHANGED
@@ -41,4 +41,16 @@ class TestToH < Test::Unit::TestCase
|
|
41
41
|
def test_converts_nested_odd_array_into_hash
|
42
42
|
assert_equal({1 => 2, 3 => nil}, [[1, 2], [3]].to_h)
|
43
43
|
end
|
44
|
+
|
45
|
+
def test_converts_nested_array_with_one_or_two_items
|
46
|
+
assert_equal({1 => nil}, [[1]].to_h)
|
47
|
+
assert_equal({1 => 2}, [[1, 2]].to_h)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_ignores_nested_array_without_one_or_two_items
|
51
|
+
assert_equal({}, [[]].to_h) # zero items
|
52
|
+
assert_equal({}, [[1, 2, 3]].to_h)
|
53
|
+
assert_equal({}, [[1, 2, 3, 4]].to_h)
|
54
|
+
assert_equal({}, [[1, 2, 3, 4, 5]].to_h) # and so on
|
55
|
+
end
|
44
56
|
end
|