static mrb_value mrb_hash_shift(mrb_state *mrb, mrb_value hash) { khash_t(ht) *h = RHASH_TBL(hash); khiter_t k; mrb_value delKey, delVal; mrb_hash_modify(mrb, hash); if (h) { if (kh_size(h) > 0) { for (k = kh_begin(h); k != kh_end(h); k++) { if (!kh_exist(h,k)) continue; delKey = kh_key(h,k); mrb_gc_protect(mrb, delKey); delVal = mrb_hash_delete_key(mrb, hash, delKey); mrb_gc_protect(mrb, delVal); return mrb_assoc_new(mrb, delKey, delVal); } } } if (MRB_RHASH_PROCDEFAULT_P(hash)) { return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, mrb_nil_value()); } else { return RHASH_IFNONE(hash); } }
/* * 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" * */ static mrb_value mrb_hash_delete(mrb_state *mrb, mrb_value self) { mrb_value key; mrb_get_args(mrb, "o", &key); return mrb_hash_delete_key(mrb, self, key); }
void nstringhash_delete(nstringhash *hash, const char *key) { mrb_hash_delete_key(__LIBN_R, hash->_hash, mrb_str_new_cstr(__LIBN_R, key)); }