delete(p1)
public
Deletes the key-value pair corresponding to the given key. If the key exists, the deleted value will be
returned, otherwise nil.
If a block is provided, the deleted key and value will be passed to the
block as arguments. If the key does
not exist in the database, the value will be nil.
Show source
static VALUE
fsdbm_delete(VALUE obj, VALUE keystr)
{
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
VALUE valstr;
fdbm_modify(obj);
ExportStringValue(keystr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
if (rb_block_given_p()) return rb_yield(keystr);
return Qnil;
}
/* need to save value before sdbm_delete() */
valstr = rb_external_str_new(value.dptr, value.dsize);
if (sdbm_delete(dbm, key)) {
dbmp->di_size = -1;
rb_raise(rb_eDBMError, "dbm_delete failed");
}
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
return valstr;
}