Issue #11818 has been updated by Tsuyoshi Sawada.


Sorry, the code was invalid. It should be:

~~~
h1 = {a: 1, b: nil, c: 2}
h1.compact # => {a: 1, c: 2}
h1 # => {a: 1, b: nil, c: 2}

h2 = {a: 1, b: nil, c: 2}
h2.compact! # => {a: 1, c: 2}
h2 # => {a: 1, c: 2}

h3 = {a: 1, c: 2}
h3.compact! # => nil
h3 # => {a: 1, c: 2}

~~~

----------------------------------------
Feature #11818: `Hash#compact`
https://bugs.ruby-lang.org/issues/11818#change-55545

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
I request `Hash#compact` and `Hash#compact!` that remove the key-value pairs whose value is `nil`, as follows:

    h1 = {a:, 1, b: nil, c: 2}
    h1.compact # => {a: 1, c: 2}
    h1 # => {a: 1, b: nil, c: 2}

    h2 = {a:, 1, b: nil, c: 2}
    h2.compact! # => {a: 1, c: 2}
    h2 # => {a: 1, c: 2}

    h3 = {a:, 1, c: 2}
    h3.compact! # => nil
    h3 # => {a: 1, c: 2}

I believe people have frequent need to do this.



-- 
https://bugs.ruby-lang.org/