From 658a00ba4927b433420c9b26dcbc2365fb657f2b Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 10 May 2014 10:18:15 +0900 Subject: Change to raise TypeError (Hash#merge, #merge!) --- mrbgems/mruby-hash-ext/mrblib/hash.rb | 2 +- mrbgems/mruby-hash-ext/test/hash.rb | 4 ++++ mrblib/hash.rb | 2 +- test/t/hash.rb | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 259b0fa12..71a518238 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -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] diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb index b9992fa96..62cfc8856 100644 --- a/mrbgems/mruby-hash-ext/test/hash.rb +++ b/mrbgems/mruby-hash-ext/test/hash.rb @@ -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 diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 5828a13eb..315a0c86e 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -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 diff --git a/test/t/hash.rb b/test/t/hash.rb index 2ddd33316..0e035a3a7 100644 --- a/test/t/hash.rb +++ b/test/t/hash.rb @@ -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 -- cgit v1.2.3