shift()
public
Removes a key-value pair from hsh and returns it as the two-item
array [ key, value ], or the hash’s default value if the hash is empty.
h = { 1 => "a", 2 => "b", 3 => "c" }
h.shift
h
Show source
static VALUE
rb_hash_shift(VALUE hash)
{
struct shift_var var;
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl) {
var.key = Qundef;
if (RHASH_ITER_LEV(hash) == 0) {
if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
return rb_assoc_new(var.key, var.val);
}
}
else {
rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
if (var.key != Qundef) {
rb_hash_delete_key(hash, var.key);
return rb_assoc_new(var.key, var.val);
}
}
}
return hash_default_value(hash, Qnil);
}