From bbd58fa2b956a33c035dbddc83e9efbe07ee7276 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 13 Oct 2016 08:06:00 +0000 Subject: 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56414 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_hash.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index edc0eeebee..7720b6098e 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -354,6 +354,14 @@ class TestHash < Test::Unit::TestCase assert_equal({1=>2,3=>4,5=>6}, h.keep_if{true}) end + def test_compact + h = @cls[a: 1, b: nil, c: false, d: true, e: nil] + assert_equal({a: 1, c: false, d: true}, h.compact) + assert_equal({a: 1, b: nil, c: false, d: true, e: nil}, h) + h.compact! + assert_equal({a: 1, c: false, d: true}, h) + end + def test_dup for taint in [ false, true ] for frozen in [ false, true ] -- cgit v1.2.3