Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Hash#except
(*::Hash::_Key keys) -> ::Hash[K, V]
except(*keys) → new_hash
hash.c
View on GitHub ↗
static VALUE
rb_hash_except(int argc, VALUE *argv, VALUE hash)
{
int i;
VALUE key, result;
result = hash_dup_with_compare_by_id(hash);
for (i = 0; i < argc; i++) {
key = argv[i];
rb_hash_delete(result, key);
}
compact_after_delete(result);
return result;
}
Returns a copy of self that excludes entries for the given keys; any keys that are not found are ignored:
Example
h = {foo:0, bar: 1, baz: 2} # => {foo: 0, bar: 1, baz: 2} h.except(:baz, :foo) # => {bar: 1} h.except(:bar, :nosuch) # => {foo: 0, baz: 2}
Related: see Methods for Deleting.
Ruby version changes
-
3.0
-
Hash#excepthas been added, which returns a hash excluding the given keys and their values. [Feature #15822]
-