Project

General

Profile

Actions

Bug #13855

closed

Hash#compact! returns nil if the hash is empty

Added by elandesign (Paul Smith) over 6 years ago. Updated over 6 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
[ruby-core:82591]

Description

This behaviour feels like a bug to me.

From the documentation (with my emphasis):

compact! → hsh
Removes all nil values from the hash. Returns the hash.

However if the hash contains no keys, the method returns nil.

irb(main):001:0> {}.compact!
=> nil

# For Comparison
irb(main):002:0> { foo: nil }.compact!
=> {}
irb(main):003:0> {}.compact
=> {}
irb(main):004:0> { foo: nil }.compact
=> {}
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0