summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSayed Abdelhaleem <[email protected]>2016-01-27 21:17:33 +0200
committerSayed Abdelhaleem <[email protected]>2016-01-27 21:17:33 +0200
commitf5f48d9400420617fa8bee3b53075894b3a53c1c (patch)
tree4d08f9594cee3a4ac9a414bcf0bafcd048ba5c49
parent2723b10a01d5a1f50c174a6ec50582ae798aa9ae (diff)
downloadmruby-f5f48d9400420617fa8bee3b53075894b3a53c1c.tar.gz
mruby-f5f48d9400420617fa8bee3b53075894b3a53c1c.zip
protect NoMethodError from calling to_hash in replace
-rw-r--r--mrblib/hash.rb1
-rw-r--r--test/t/hash.rb4
2 files changed, 5 insertions, 0 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb
index e3e709070..782111459 100644
--- a/mrblib/hash.rb
+++ b/mrblib/hash.rb
@@ -154,6 +154,7 @@ class Hash
#
# ISO 15.2.13.4.23
def replace(hash)
+ raise TypeError, "can't convert argument into Hash" unless hash.respond_to?(:to_hash)
self.clear
hash = hash.to_hash
hash.each_key{|k|
diff --git a/test/t/hash.rb b/test/t/hash.rb
index 3196cc97a..c8d7a70ef 100644
--- a/test/t/hash.rb
+++ b/test/t/hash.rb
@@ -239,6 +239,10 @@ assert('Hash#replace', '15.2.13.4.23') do
a = Hash.new{|h,x| x}
b.replace(a)
assert_equal(127, b[127])
+
+ assert_raise(TypeError) do
+ { 'abc_key' => 'abc_value' }.replace "a"
+ end
end
assert('Hash#shift', '15.2.13.4.24') do