Method: Hash#reject
- Defined in:
- hash.c
#reject {|key, value| ... } ⇒ Object #reject ⇒ Object
Returns a new Hash object whose entries are all those from self for which the block returns false or nil:
h = {foo: 0, bar: 1, baz: 2}
h1 = h.reject {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
Returns a new Enumerator if no block given:
h = {foo: 0, bar: 1, baz: 2}
e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
h1 = e.each {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 |
# File 'hash.c', line 2609
static VALUE
rb_hash_reject(VALUE hash)
{
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
result = hash_dup_with_compare_by_id(hash);
if (!RHASH_EMPTY_P(hash)) {
rb_hash_foreach(result, delete_if_i, result);
compact_after_delete(result);
}
return result;
}
|