Method: Hash#reject

Defined in:
hash.c

#reject {|key, value| ... } ⇒ Hash #rejectObject

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"}  #=> {"b" => 200, "c" => 300}
h.reject {|k,v| v > 100}  #=> {"a" => 100}

Overloads:

  • #reject {|key, value| ... } ⇒ Hash

    Yields:

    Returns:



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'hash.c', line 1128

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_MISC_ATTRIBUTES(hash, klass)) {
#if HASH_REJECT_COPY_MISC_ATTRIBUTES
	    rb_warn("copying unguaranteed attributes: %+"PRIsVALUE, hash);
	    rb_warn("following atributes will not be copied in the future version:");
	    if (klass != rb_cHash) {
		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));
#else
	    rb_warn("unguaranteed attributes are not copied: %+"PRIsVALUE, hash);
	    rb_warn("following atributes are ignored now:");
#endif
	}
    }
#if HASH_REJECT_COPY_MISC_ATTRIBUTES
    result = rb_hash_dup_empty(hash);
#else
    result = rb_hash_new();
#endif
    if (!RHASH_EMPTY_P(hash)) {
	rb_hash_foreach(hash, reject_i, result);
    }
    return result;
}