/*
* call-seq:
* hsh.delete(key) => value
* hsh.delete(key) {| key | block } => value
*
* Deletes and returns a key-value pair from <i>hsh</i> whose key is
* equal to <i>key</i>. If the key is not found, returns the
* <em>default value</em>. If the optional code block is given and the
* key is not found, pass in the key and return the result of
* <i>block</i>.
*
* h = { "a" => 100, "b" => 200 }
* h.delete("a") #=> 100
* h.delete("z") #=> nil
* h.delete("z") { |el| "#{el} not found" } #=> "z not found"
*
*/
VALUE
rb_hash_delete(hash, key)
VALUE hash, key;
{
VALUE val;
rb_hash_modify(hash);
if (RHASH(hash)->iter_lev > 0) {
if (st_delete_safe(RHASH(hash)->tbl, (st_data_t*)&key, &val, Qundef)) {
FL_SET(hash, HASH_DELETED);
return val;
}
}
else if (st_delete(RHASH(hash)->tbl, (st_data_t*)&key, &val))
return val;
if (rb_block_given_p()) {
return rb_yield(key);
}
return Qnil;
}