Project

General

Profile

Bug #12863

Hash#compact! behavior different from the Active Support Hash#compact!

Added by cha1tanya (Prathamesh Sonpatki) 9 months ago. Updated 9 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:77709]

Description

Hash#compact and Hash#compact! were added in https://bugs.ruby-lang.org/issues/11818 but the Hash#compact! is different from Active Support. Please check following snippet:

# Active Support
>> hash = { a: true, b: false, c: nil }
=> {:a=>true, :b=>false, :c=>nil}
>> hash.compact!
=> {:a=>true, :b=>false}
>> hash.compact!
=> nil

# Ruby dev

>> hash = { a: true, b: false, c: nil }
=> {:a=>true, :b=>false, :c=>nil}
>> hash.compact!
=> {:a=>true, :b=>false}
>> hash.compact!
=> {:a=>true, :b=>false}
>> 

nobu (Nobuyoshi Nakada) Was it intentional to not return nil in case of compact! when none of the values are nil?

Associated revisions

Revision 56473
Added by nobu (Nobuyoshi Nakada) 9 months ago

hash.c: fix Hash#compact! return value

* hash.c (rb_hash_compact_bang): should return nil if no elements
is deleted. [Bug #12863]

Revision 56473
Added by nobu (Nobuyoshi Nakada) 9 months ago

hash.c: fix Hash#compact! return value

* hash.c (rb_hash_compact_bang): should return nil if no elements
is deleted. [Bug #12863]

History

#1 [ruby-core:77710] Updated by zverok (Victor Shepelev) 9 months ago

What is the reason of returning nil in that case? I believe in standard containers all bang-versions always return self?

#2 [ruby-core:77711] Updated by zverok (Victor Shepelev) 9 months ago

Hm, looks like returning nil (if nothing was dropped) is standard behavior of Hash#reject!. Weird.

#3 [ruby-core:77712] Updated by nobu (Nobuyoshi Nakada) 9 months ago

  • Backport set to 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
  • Tracker changed from Misc to Bug

#4 Updated by nobu (Nobuyoshi Nakada) 9 months ago

  • Status changed from Open to Closed

Applied in changeset r56473.


hash.c: fix Hash#compact! return value

  • hash.c (rb_hash_compact_bang): should return nil if no elements is deleted. [Bug #12863]

Also available in: Atom PDF