Issue #14097 has been updated by jeremyevans0 (Jeremy Evans).


ana06 (Ana Maria Martinez Gomez) wrote:
> What about introducing `concat!` with the same behaviour as `concat` and deprecating `concat`. Then we could in the feature give `concat` the behaviour it deserves. It is confusing as well that this method modify the object and I think we should fix this.

Regarding `concat!`, there seems to be a misunderstanding that methods should end with `!` to be mutating.  That is not the convention in the core classes.  The core classes have many methods that are mutating but do not end in `!`.  The convention regarding `!` is if there is both a method with `!` and a method without, the version with `!` mutates and the version without returns a potentially modified copy.

The following Array methods that do not end in `!` are mutating, so if you want to change `concat` for "consistency", you would have to change all of them:

* clear
* concat
* delete
* delete_at
* delete_if
* fill
* insert
* keep_if
* pop
* push
* replace
* shift
* unshift

You'd also probably have to change String and Hash similarly if you wanted this "consistency" in regards to `!`.

In regards to `union`, not all arrays are sets, and I'm not in favor of introducing additional set-specific methods to Array.  `Set#union` is already implemented.

----------------------------------------
Bug #14097: Add union and difference to Array
https://bugs.ruby-lang.org/issues/14097#change-67781

* Author: ana06 (Ana Maria Martinez Gomez)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Currently there is a concat method in ruby which does the same as +, but modifying the object. We could introduce a union and difference methods, which would be the equivalent for the `|` and `-` operators. This operators are normally less used due to lack of readability and expressivity. You end seeing thinks like:

```
array.concat(array2).uniq!
```

just because it is more readable. When it could be written like:

```
array |= array2
```

But, as this is not clear for some people, the new method will solve this problem:

```
array.union(array2)
```

And now this is clean and readable, as everybody expect from Ruby, the language focused on simplicity and productivity. ;)


Can I send a PR? :)





-- 
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>