mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #2226 from yui-knk/hash-merge-error
Change to raise TypeError (Hash#merge, #merge!)
This commit is contained in:
@@ -22,7 +22,7 @@ class Hash
|
||||
#
|
||||
|
||||
def merge!(other, &block)
|
||||
raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
|
||||
raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
|
||||
if block
|
||||
other.each_key{|k|
|
||||
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
|
||||
|
||||
@@ -16,6 +16,10 @@ assert('Hash#merge!') do
|
||||
'xyz_key' => 'xyz_value' }, result_1)
|
||||
assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
|
||||
'xyz_key' => 'xyz_value' }, result_2)
|
||||
|
||||
assert_raise(TypeError) do
|
||||
{ 'abc_key' => 'abc_value' }.merge! "a"
|
||||
end
|
||||
end
|
||||
|
||||
assert('Hash#values_at') do
|
||||
|
||||
+1
-1
@@ -179,7 +179,7 @@ class Hash
|
||||
# ISO 15.2.13.4.22
|
||||
def merge(other, &block)
|
||||
h = {}
|
||||
raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
|
||||
raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
|
||||
other = other.to_hash
|
||||
self.each_key{|k| h[k] = self[k]}
|
||||
if block
|
||||
|
||||
@@ -223,6 +223,10 @@ assert('Hash#merge', '15.2.13.4.22') do
|
||||
'xyz_key' => 'xyz_value' }, result_1)
|
||||
assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value',
|
||||
'xyz_key' => 'xyz_value' }, result_2)
|
||||
|
||||
assert_raise(TypeError) do
|
||||
{ 'abc_key' => 'abc_value' }.merge "a"
|
||||
end
|
||||
end
|
||||
|
||||
assert('Hash#replace', '15.2.13.4.23') do
|
||||
|
||||
Reference in New Issue
Block a user