mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
ac2683c809
commit
498838c662
4 changed files with 53 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue