Issue #13855 has been updated by elandesign (Paul Smith).


lucasbuchala (Lucas Buchala) wrote:
> Rather than a bug, I wonder if this is just a documentation omission.
> 
> In case of a documentation omission, I created a PR:
> 
> https://github.com/ruby/ruby/pull/1692

Oh that's interesting - I hadn't noticed that returning nil if the object was unchanged was the actual behaviour there. I agree it could be seen as a documentation issue - honestly, I'd be happier with the bang method returning the affected object, but so long as the behaviour is expected either way works for me. 

----------------------------------------
Bug #13855: Hash#compact! returns nil if the hash is empty
https://bugs.ruby-lang.org/issues/13855#change-66415

* Author: elandesign (Paul Smith)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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
=> {}
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>