reject()
public
Returns a new hash consisting of entries for which the block
returns false.
If no block is given, an enumerator is returned instead.
h = { "a" => 100, "b" => 200, "c" => 300 }
h.reject {|k,v| k < "b"}
h.reject {|k,v| v > 100}
Show source
VALUE
rb_hash_reject(VALUE hash)
{
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
if (RTEST(ruby_verbose)) {
VALUE klass;
if (HAS_EXTRA_STATES(hash, klass)) {
rb_warn("copying extra states: %+"PRIsVALUE, hash);
rb_warn("following states will not be copied in the future version:");
if (klass) {
rb_warn(" subclass: %+"PRIsVALUE, klass);
}
if (FL_TEST(hash, FL_EXIVAR)) {
rb_warn(" instance variables: %+"PRIsVALUE,
rb_obj_instance_variables(hash));
}
if (FL_TEST(hash, FL_TAINT)) {
rb_warn(" taintedness");
}
if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
rb_warn(" default proc: %+"PRIsVALUE, RHASH_IFNONE(hash));
}
else if (!NIL_P(RHASH_IFNONE(hash)))
rb_warn(" default value: %+"PRIsVALUE, RHASH_IFNONE(hash));
rb_warn("extra states are no longer copied: %+"PRIsVALUE, hash);
}
}
result = rb_hash_dup_empty(hash);
result = rb_hash_new();
if (!RHASH_EMPTY_P(hash)) {
rb_hash_foreach(hash, reject_i, result);
}
return result;
}