Issue #11818 has been updated by Dwain Faithfull.


Prem Sichanugrist wrote:
> Active Support has this: http://api.rubyonrails.org/classes/Hash.html#method-i-compact
> 
> I'm +1 on porting this method. While I think it's possible using a block form and `delete_if`, this method has a good name and intention that could live by its own.

https://github.com/ruby/ruby/pull/1184/files

In anticipation of this being approved, I have raised a PR of a sample implementation, along with some benchmarks against the Active Support implementation and tests.

This is my first time contributing to Ruby, so I apologise if I've jumped the gun or done anything wrong - seemed like a good issue to pick up for someone new to the codebase, with no possible breaking changes, and some good performance gains to be had. 

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

* 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/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>