Permalink
Browse files

add support for Enumerable and Enumerator #to_h

  • Loading branch information...
1 parent b7de0ae commit a146f66594caffd6c1b68d864d729a870e516975 @sunaku committed Sep 27, 2011
Showing with 11 additions and 1 deletion.
  1. +1 −1 lib/equal_rights_for_hash.rb
  2. +10 −0 test/to_h_test.rb
View
2 lib/equal_rights_for_hash.rb
@@ -8,7 +8,7 @@ class Object
def to_h
if respond_to? :to_hash
to_hash
- elsif respond_to? :to_ary and not all? {|item| item.respond_to? :to_ary }
+ elsif respond_to? :all? and not all? {|item| item.respond_to? :to_ary }
Hash[*self]
else
Hash[self]
View
10 test/to_h_test.rb
@@ -53,4 +53,14 @@ def test_ignores_nested_array_without_one_or_two_items
assert_equal({}, [[1, 2, 3, 4]].to_h)
assert_equal({}, [[1, 2, 3, 4, 5]].to_h) # and so on
end
+
+ def test_works_on_enumerables
+ assert_equal({1 => 2, 3 => 4}, (1..4).to_a.to_h)
+ assert_equal({1 => 2, 3 => 4}, (1..4).to_h)
+ end
+
+ def test_works_on_enumerators
+ assert_equal({1 => 2, 3 => 4}, (1..4).to_enum.to_a.to_h)
+ assert_equal({1 => 2, 3 => 4}, (1..4).to_enum.to_h)
+ end
end

0 comments on commit a146f66

Please sign in to comment.