summaryrefslogtreecommitdiffhomepage
path: root/mrblib/hash.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-07-17 10:35:41 +0900
committerGitHub <[email protected]>2019-07-17 10:35:41 +0900
commitd605b72c1d6fa4564a0a5e88535504b6850463b5 (patch)
tree774fc0de56002abb3bb2b1c3387ff08f91876d17 /mrblib/hash.rb
parent2af92d0ebcbeca6d3d85a27c8193273080a63090 (diff)
parent9af3b7c6258de327218dd04e69d76ae68caf17b1 (diff)
downloadmruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.tar.gz
mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.zip
Merge branch 'master' into i110/inspect-recursion
Diffstat (limited to 'mrblib/hash.rb')
-rw-r--r--mrblib/hash.rb60
1 files changed, 16 insertions, 44 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb
index 6fcbba173..0e2da62e1 100644
--- a/mrblib/hash.rb
+++ b/mrblib/hash.rb
@@ -12,9 +12,7 @@ class Hash
# ISO 15.2.13.4.1
def ==(hash)
return true if self.equal?(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
+ unless Hash === hash
return false
end
return false if self.size != hash.size
@@ -32,9 +30,7 @@ class Hash
# ISO 15.2.13.4.32 (x)
def eql?(hash)
return true if self.equal?(hash)
- begin
- hash = hash.to_hash
- rescue NoMethodError
+ unless Hash === hash
return false
end
return false if self.size != hash.size
@@ -55,10 +51,9 @@ class Hash
# ISO 15.2.13.4.8
def delete(key, &block)
if block && !self.has_key?(key)
- block.call(key)
- else
- self.__delete(key)
+ return block.call(key)
end
+ self.__delete(key)
end
##
@@ -154,9 +149,8 @@ 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)
+ raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash
self.clear
- hash = hash.to_hash
hash.each_key{|k|
self[k] = hash[k]
}
@@ -179,8 +173,7 @@ class Hash
#
# ISO 15.2.13.4.22
def merge(other, &block)
- raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
- other = other.to_hash
+ raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
h = self.dup
if block
other.each_key{|k|
@@ -197,9 +190,16 @@ class Hash
return "{}" if self.size == 0
return "{...}" if recur_list[self.object_id]
recur_list[self.object_id] = true
- "{"+self.map {|k,v|
- k._inspect(recur_list) + "=>" + v._inspect(recur_list)
- }.join(", ")+"}"
+ ary=[]
+ keys=self.keys
+ size=keys.size
+ i=0
+ while i<size
+ k=keys[i]
+ ary<<(k._inspect + "=>" + self[k]._inspect)
+ i+=1
+ end
+ "{"+ary.join(", ")+"}"
end
##
# Return the contents of this hash as a string.
@@ -316,34 +316,6 @@ class Hash
}
h
end
-
- ##
- # call-seq:
- # hsh.rehash -> hsh
- #
- # Rebuilds the hash based on the current hash values for each key. If
- # values of key objects have changed since they were inserted, this
- # method will reindex <i>hsh</i>.
- #
- # h = {"AAA" => "b"}
- # h.keys[0].chop!
- # h #=> {"AA"=>"b"}
- # h["AA"] #=> nil
- # h.rehash #=> {"AA"=>"b"}
- # h["AA"] #=> "b"
- #
- def rehash
- h = {}
- self.each{|k,v|
- h[k] = v
- }
- self.replace(h)
- end
-
- def __update(h)
- h.each_key{|k| self[k] = h[k]}
- self
- end
end
##