Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement #to_h along with unit test

  • Loading branch information...
commit 765f034e033df9d6b9d09773c047087e518f2408 1 parent 97ec2b2
@sunaku authored
View
2  equal_rights_for_hash.gemspec
@@ -4,7 +4,7 @@ require "equal_rights_for_hash/version"
Gem::Specification.new do |s|
s.name = "equal_rights_for_hash"
- s.version = EqualRights::VERSION
+ s.version = EqualRightsForHash::VERSION
s.authors = ["Suraj N. Kurapati"]
s.email = ["sunaku@gmail.com"]
s.homepage = "http://redmine.ruby-lang.org/issues/5008"
View
27 lib/equal_rights_for_hash.rb
@@ -0,0 +1,27 @@
+require "equal_rights_for_hash/version"
+
+module Kernel
+ def Hash(value)
+ value.to_h
+ end
+end
+
+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 and item.size <= 2 }
+ then
+ Hash[*self]
+ else
+ Hash[self]
+ end
+ end
+end
+
+class NilClass
+ def to_h
+ {}
+ end
+end
View
3  lib/equal_rights_for_hash/version.rb
@@ -0,0 +1,3 @@
+module EqualRightsForHash
+ VERSION = "0.0.1"
+end
View
15 lib/kernel_hash.rb
@@ -1,15 +0,0 @@
-# See http://redmine.ruby-lang.org/issues/show/3131
-
-module Kernel
- unless method_defined? :Hash
- def Hash(value)
- if value.respond_to? :to_hash
- value.to_hash
- elsif value.nil? or Array(value).empty?
- {}
- else
- raise TypeError, "invalid value for Hash: #{value}"
- end
- end
- end
-end
View
11 test/kernel_hash_test.rb
@@ -1,15 +1,15 @@
require "test/unit"
-require "kernel_hash"
+require "equal_rights_for_hash"
-class Testkernel_hash < Test::Unit::TestCase
+class TestKernelHash < Test::Unit::TestCase
def test_converts_nil_into_hash
assert_equal({}, Hash(nil))
end
- def test_converts_real_hash_into_hash
+ def test_converts_real_hash_into_self
real_hash = {:real => true}
- assert_equal(real_hash, Hash(real_hash))
+ assert_same(real_hash, Hash(real_hash))
end
def test_converts_fake_hash_into_hash
@@ -17,7 +17,10 @@ def test_converts_fake_hash_into_hash
def fake_hash.to_hash
{:fake => true}
end
+ assert_kind_of(Hash, Hash(fake_hash))
+ assert_equal({:fake => true}, Hash(fake_hash))
assert_equal(fake_hash.to_hash, Hash(fake_hash))
+ assert_equal(fake_hash.to_h, Hash(fake_hash))
end
def test_converts_empty_array_into_hash
View
44 test/to_h_test.rb
@@ -0,0 +1,44 @@
+require "test/unit"
+
+require "equal_rights_for_hash"
+
+class TestToH < Test::Unit::TestCase
+ def test_converts_nil_into_hash
+ assert_equal({}, nil.to_h)
+ end
+
+ def test_converts_real_hash_into_self
+ real_hash = {:real => true}
+ assert_same(real_hash, real_hash.to_h)
+ end
+
+ def test_converts_fake_hash_into_hash
+ fake_hash = Object.new
+ def fake_hash.to_h
+ {:fake => true}
+ end
+ assert_kind_of(Hash, fake_hash.to_h)
+ assert_equal({:fake => true}, fake_hash.to_h)
+ end
+
+ def test_converts_empty_array_into_hash
+ assert_equal({}, [].to_h)
+ end
+
+ def test_converts_flat_even_array_into_hash
+ assert_equal({1 => 2, 3 => 4}, [1, 2, 3, 4].to_h)
+ end
+
+ def test_converts_nested_even_array_into_hash
+ assert_equal({1 => 2, 3 => 4}, [[1, 2], [3, 4]].to_h)
+ end
+
+ def test_converts_flat_odd_array_into_hash
+ error = assert_raise(ArgumentError) { [1, 2, 3].to_h }
+ assert_equal("odd number of arguments for Hash", error.message)
+ end
+
+ def test_converts_nested_odd_array_into_hash
+ assert_equal({1 => 2, 3 => nil}, [[1, 2], [3]].to_h)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.