method
slice
ruby latest stable - Class:
Hash
slice(*args)public
Returns a hash containing only the given keys and their values.
h = { a: 100, b: 200, c: 300 } h.slice (:a) #=> {:a=>100} h.slice (:b, :c, :d) #=> {:b=>200, :c=>300}
static VALUE
rb_hash_slice(int argc, VALUE *argv, VALUE hash)
{
int i;
VALUE key, value, result;
if (argc == 0 || RHASH_EMPTY_P(hash)) {
return rb_hash_new();
}
result = rb_hash_new_with_size(argc);
for (i = 0; i < argc; i++) {
key = argv[i];
value = rb_hash_lookup2(hash, key, Qundef);
if (value != Qundef)
rb_hash_aset(result, key, value);
}
return result;
}