7 # Equality---Two hashes are equal if they each contain the same number
8 # of keys and if each key-value pair is equal to (according to
9 # <code>Object#==</code>) the corresponding elements in the other
14 return true if self.equal?(hash)
18 return false if self.size != hash.size
20 return false unless hash.key?(k)
21 return false unless self[k] == hash[k]
27 # Returns <code>true</code> if <i>hash</i> and <i>other</i> are
28 # both hashes with the same content compared by eql?.
30 # ISO 15.2.13.4.32 (x)
32 return true if self.equal?(hash)
36 return false if self.size != hash.size
38 return false unless hash.key?(k)
39 return false unless self[k].eql?(hash[k])
45 # Delete the element with the key +key+.
46 # Return the value of the element if +key+
47 # was found. Return nil if nothing was
48 # found. If a block is given, call the
49 # block with the value of the element.
52 def delete(key, &block)
53 if block && !self.has_key?(key)
54 return block.call(key)
60 # Calls the given block for each element of +self+
61 # and pass the key and value of each element.
64 # hsh.each {| key, value | block } -> hsh
65 # hsh.each_pair {| key, value | block } -> hsh
66 # hsh.each -> an_enumerator
67 # hsh.each_pair -> an_enumerator
70 # If no block is given, an enumerator is returned instead.
72 # h = { "a" => 100, "b" => 200 }
73 # h.each {|key, value| puts "#{key} is #{value}" }
82 return to_enum :each unless block
89 block.call [keys[i], vals[i]]
96 # Calls the given block for each element of +self+
97 # and pass the key of each element.
100 # hsh.each_key {| key | block } -> hsh
101 # hsh.each_key -> an_enumerator
103 # If no block is given, an enumerator is returned instead.
105 # h = { "a" => 100, "b" => 200 }
106 # h.each_key {|key| puts key }
115 return to_enum :each_key unless block
117 self.keys.each{|k| block.call(k)}
122 # Calls the given block for each element of +self+
123 # and pass the value of each element.
126 # hsh.each_value {| value | block } -> hsh
127 # hsh.each_value -> an_enumerator
129 # If no block is given, an enumerator is returned instead.
131 # h = { "a" => 100, "b" => 200 }
132 # h.each_value {|value| puts value }
140 def each_value(&block)
141 return to_enum :each_value unless block
143 self.values.each{|v| block.call(v)}
148 # Replaces the contents of <i>hsh</i> with the contents of other hash
152 raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash
158 self.default_proc = hash.default_proc
160 self.default = hash.default
165 alias initialize_copy replace
168 # Return a hash which contains the content of
169 # +self+ and +other+. If a block is given
170 # it will be called for each element with
171 # a duplicate key. The value of the block
172 # will be the final value of this element.
175 def merge(other, &block)
176 raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
180 h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
183 other.each_key{|k| h[k] = other[k]}
188 # internal method for Hash inspection
189 def _inspect(recur_list)
190 return "{}" if self.size == 0
191 return "{...}" if recur_list[self.object_id]
192 recur_list[self.object_id] = true
199 ary<<(keys[i]._inspect(recur_list) + "=>" + vals[i]._inspect(recur_list))
202 "{"+ary.join(", ")+"}"
205 # Return the contents of this hash as a string.
207 # ISO 15.2.13.4.30 (x)
211 # ISO 15.2.13.4.31 (x)
216 # hsh.reject! {| key, value | block } -> hsh or nil
217 # hsh.reject! -> an_enumerator
219 # Equivalent to <code>Hash#delete_if</code>, but returns
220 # <code>nil</code> if no changes were made.
222 # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
225 return to_enum :reject! unless block
229 if block.call([k, v])
233 return nil if keys.size == 0
242 # hsh.reject {|key, value| block} -> a_hash
243 # hsh.reject -> an_enumerator
245 # Returns a new hash consisting of entries for which the block returns false.
247 # If no block is given, an enumerator is returned instead.
249 # h = { "a" => 100, "b" => 200, "c" => 300 }
250 # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
251 # h.reject {|k,v| v > 100} #=> {"a" => 100}
253 # 1.8/1.9 Hash#reject returns Hash; ISO says nothing.
256 return to_enum :reject unless block
260 unless block.call([k, v])
269 # hsh.select! {| key, value | block } -> hsh or nil
270 # hsh.select! -> an_enumerator
272 # Equivalent to <code>Hash#keep_if</code>, but returns
273 # <code>nil</code> if no changes were made.
275 # 1.9 Hash#select! returns Hash; ISO says nothing.
278 return to_enum :select! unless block
282 unless block.call([k, v])
286 return nil if keys.size == 0
295 # hsh.select {|key, value| block} -> a_hash
296 # hsh.select -> an_enumerator
298 # Returns a new hash consisting of entries for which the block returns true.
300 # If no block is given, an enumerator is returned instead.
302 # h = { "a" => 100, "b" => 200, "c" => 300 }
303 # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
304 # h.select {|k,v| v < 200} #=> {"a" => 100}
306 # 1.9 Hash#select returns Hash; ISO says nothing
309 return to_enum :select unless block
313 if block.call([k, v])