From 498838c6627c82befd3c6dcd5c5d6870b38db5e7 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 24 Jan 2012 04:02:30 +0000 Subject: * object.c (rb_Hash): add Kernel#Hash conversion method like Array() or Float(). a patch from Run Paint Run Run. Fix #3131 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34367 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_object.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/ruby/test_object.rb') diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index b05375ebba..15e7b72e35 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -197,6 +197,19 @@ class TestObject < Test::Unit::TestCase assert_equal([o], Array(o)) end + def test_convert_hash + assert_equal(Hash(nil), {}) + assert_equal(Hash([]), {}) + assert_equal(Hash(key: :value), {key: :value}) + assert_raise(TypeError) { Hash([1,2]) } + assert_raise(TypeError) { Hash(Object.new) } + o = Object.new + def o.to_hash; {a: 1, b: 2}; end + assert_equal(Hash(o), {a: 1, b: 2}) + def o.to_hash; 9; end + assert_raise(TypeError) { Hash(o) } + end + def test_to_integer o = Object.new def o.to_i; nil; end -- cgit v1.2.3