ruby-changes:44400
From: nobu <ko1@a...>
Date: Sat, 22 Oct 2016 18:52:40 +0900 (JST)
Subject: [ruby-changes:44400] nobu:r56473 (trunk): hash.c: fix Hash#compact! return value
nobu 2016-10-22 18:52:35 +0900 (Sat, 22 Oct 2016) New Revision: 56473 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56473 Log: hash.c: fix Hash#compact! return value * hash.c (rb_hash_compact_bang): should return nil if no elements is deleted. [ruby-core:77709] [Bug #12863] Modified files: trunk/ChangeLog trunk/hash.c trunk/test/ruby/test_hash.rb Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 56472) +++ test/ruby/test_hash.rb (revision 56473) @@ -358,8 +358,9 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L358 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_same(h, h.compact!) assert_equal({a: 1, c: false, d: true}, h) + assert_nil(h.compact!) end def test_dup Index: hash.c =================================================================== --- hash.c (revision 56472) +++ hash.c (revision 56473) @@ -2726,9 +2726,12 @@ rb_hash_compact_bang(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2726 { rb_hash_modify_check(hash); if (RHASH(hash)->ntbl) { + st_index_t n = RHASH(hash)->ntbl->num_entries; rb_hash_foreach(hash, delete_if_nil, hash); + if (n != RHASH(hash)->ntbl->num_entries) + return hash; } - return hash; + return Qnil; } static VALUE rb_hash_compare_by_id_p(VALUE hash); Index: ChangeLog =================================================================== --- ChangeLog (revision 56472) +++ ChangeLog (revision 56473) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Oct 22 18:52:32 2016 Nobuyoshi Nakada <nobu@r...> + + * hash.c (rb_hash_compact_bang): should return nil if no elements + is deleted. [ruby-core:77709] [Bug #12863] + Sat Oct 22 10:28:28 2016 Nobuyoshi Nakada <nobu@r...> * configure.in (DLDFLAGS): fallback to LDFLAGS. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/