Issue #14701 has been updated by sonots (Naotoshi Seo).


> Use append instead of += for arrays.

`+=` operation for NArray is totally different with `append` for ruby array.
What we want to do with `+=` operation for NArray is to do element-wise addition like

```
narray([1, 2, 3]) += narray([1, 1, 1]) #=> narray([2, 3, 4])
```

Because the memory size of a NAarray can be large such as 1GB, we wan to perform in-place operation without allocating a new NArray.

> Changing the behavior of += would have too much compatibility problems from side-effect.

It is not necessary to change behavior of ruby default.
We just want to redefine `+=` operator for a specific type of objects such as NArray. So, compatibility problems occur for only classes which redefine `+=` operator newly.


----------------------------------------
Feature #14701: If the object is not frozen, I want to be able to redefine the compound assignment operator.
https://bugs.ruby-lang.org/issues/14701#change-71641

* Author: naitoh (Jun NAITOH)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
If the object is not frozen, I want to be able to redefine the compound assignment operator (e.g. +=, -=, *=, /=, ..etc ).

https://docs.ruby-lang.org/ja/latest/doc/spec=2foperator.html

* Redefinable operator (method) 

~~~
    |  ^  &  <=>  ==  ===  =~  >   >=  <   <=   <<  >>
    +  -  *  /    %   **   ~   +@  -@  []  []=  ` ! != !~
~~~

* use case

~~~
> require 'numo/narray'
> a = Numo::Int32[5, 6]
=> Numo::Int32#shape=[2]
[5, 6]
> a.object_id
=> 70326927544920
> a += 1
=> Numo::Int32#shape=[2]
[6, 7]
> a.object_id
=> 70326927530540
> a.inplace + 1
=> Numo::Int32(view)#shape=[2]
[7, 8]
> a.object_id
=> 70326927530540
~~~

With Numo::NArray, using "inplace" instead of "+=" will update the same object so it will be faster.

I want to write "a += 1" instead of "a.inplace + 1".
However, Ruby can not redefine "+=".



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