From 28ab1062925b99118c0aa307e2c7d2a23fc0fc29 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 4 Apr 2014 10:50:42 +0900 Subject: Hash#replace to preserve order; close #2001 --- mrblib/hash.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'mrblib') diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 853f6ed97..1f2358a52 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -142,6 +142,20 @@ class Hash self.__init_core(block, *args) end + ## + # Replaces the contents of hsh with the contents of other hash + # + # ISO 15.2.13.4.23 + def replace(hash) + self.clear + hash.each_key{|k| + self[k] = hash[k] + } + self + end + # ISO 15.2.13.4.17 + alias initialize_copy replace + ## # Return a hash which contains the content of # +self+ and +other+. If a block is given -- cgit v1.2.3