Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns self with all its nil-valued entries removed (in place):
h = {foo: 0, bar: nil, baz: 2, bat: nil}
h.compact! # => {:foo=>0, :baz=>2}
Returns nil if no entries were removed.
               static VALUE
rb_hash_compact_bang(VALUE hash)
    st_index_t n;
    n = RHASH_SIZE(hash);
    if (n) {
        rb_hash_foreach(hash, delete_if_nil, hash);
        if (n != RHASH_SIZE(hash))
            return hash;
    return Qnil;

Was this page useful?