[#70843] Re: [ruby-cvs:58952] hsbt:r51801 (trunk): * lib/rubygems: Update to RubyGems HEAD(fe61e4c112). — Eric Wong <normalperson@...>
hsbt@ruby-lang.org wrote:
3 messages
2015/09/17
[ruby-core:70719] [Ruby trunk - Bug #11520] Inconsistent behavior in Array#compact!
From:
0x0dea+redmine@...
Date:
2015-09-10 23:42:02 UTC
List:
ruby-core #70719
Issue #11520 has been updated by D.E. Akers.
This behavior is consistent with many other "bang methods" which return `nil` if they might have mutated the receiver but didn't:
``` ruby
[
'foo'.downcase!,
'BAR'.upcase!,
'Baz'.capitalize!,
'foo'.strip!,
'bar'.lstrip!,
'baz'.rstrip!,
'foo'.sub!(/\d/, 'x'),
'bar'.gsub!(/\d/, 'x'),
'abc'.tr!('def', 'ghi'),
[1,2,3].compact!,
[1,2,3].flatten!,
[1,2,3].uniq!
].any? # => false
```
This idiom is a Good Thing because it provides you with greater information density than if these methods simply returned the mutated receiver, since that's something to which you already have a reference. In the common enough case that you want to know whether something changed, you can simply use the return value rather than storing a copy of the object and comparing it before and after (potential) mutation.
----------------------------------------
Bug #11520: Inconsistent behavior in Array#compact!
https://bugs.ruby-lang.org/issues/11520#change-54104
* Author: Hal Brodigan
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
I noticed that `Array#compact!` sometimes returns `self` and other times `nil`. This behavior was a bit confusing.
[].compact!
# => nil
[1].compact!
# => nil
[nil].compact!
# => []
[1, nil].compact!
# => [1]
I would prefer that `Array#compact!` either always returns `nil`, or always returns `self`.
--
https://bugs.ruby-lang.org/