Sorry, the code was invalid. It should be:
You can do this with Hash#delete_if
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.
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Accepted.
Matz.
- Status changed from Assigned to Closed
Applied in changeset r56414.
hash.c: add compact and compact! methods
- hash.c (rb_hash_compact, rb_hash_compact_bang): Removes nil
values from the original hash, to port Active Support behavior.
[Feature #11818]