Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns a copy of self with all nil-valued entries removed:
h = {foo: 0, bar: nil, baz: 2, bat: nil}
h1 = h.compact
h1 # => {:foo=>0, :baz=>2}
               static VALUE
rb_hash_compact(VALUE hash)
    VALUE result = rb_hash_new();
    if (!RHASH_EMPTY_P(hash)) {
        rb_hash_foreach(hash, set_if_not_nil, result);
    return result;

Was this page useful?