summaryrefslogtreecommitdiffhomepage
path: root/mrblib
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-04-01 14:56:23 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-04-01 14:56:23 +0900
commit1690fc4c99162aece186fd6e4f5d8fe43e34564e (patch)
tree96fb10f6f8fd2ac9c4e0151810ed6a4d53e6cb93 /mrblib
parent8b141b07b67b185caaaf2945fe9a6c1f7208e647 (diff)
downloadmruby-1690fc4c99162aece186fd6e4f5d8fe43e34564e.tar.gz
mruby-1690fc4c99162aece186fd6e4f5d8fe43e34564e.zip
implement Hash#== and eql? in Ruby
Diffstat (limited to 'mrblib')
-rw-r--r--mrblib/hash.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb
index d15fa434e..853f6ed97 100644
--- a/mrblib/hash.rb
+++ b/mrblib/hash.rb
@@ -3,6 +3,39 @@
#
# ISO 15.2.13
class Hash
+ ##
+ # Equality---Two hashes are equal if they each contain the same number
+ # of keys and if each key-value pair is equal to (according to
+ # <code>Object#==</code>) the corresponding elements in the other
+ # hash.
+ #
+ # ISO 15.2.13.4.1
+ def == (hash)
+ return true if self.equal?(hash)
+ hash = hash.to_hash
+ return false if self.size != hash.size
+ self.each do |k,v|
+ return false unless hash.key?(k)
+ return false unless self[k] == hash[k]
+ end
+ return true
+ end
+
+ ##
+ # Returns <code>true</code> if <i>hash</i> and <i>other</i> are
+ # both hashes with the same content compared by eql?.
+ #
+ # ISO 15.2.13.4.32 (x)
+ def eql?(hash)
+ return true if self.equal?(hash)
+ hash = hash.to_hash
+ return false if self.size != hash.size
+ self.each do |k,v|
+ return false unless hash.key?(k)
+ return false unless self[k].eql?(hash[k])
+ end
+ return true
+ end
##
# Delete the element with the key +key+.