Issue #14107 has been updated by nobu (Nobuyoshi Nakada).

Description updated
Status changed from Open to Rejected

fanantoxa (Anton Sivakov) wrote:
> Or even if I mutate memo directly at the end like:

It doesn't mutate the argument object.

~~~ruby
d.each_with_object([1, {}]) do |item, memo|
  index, chunks = memo
  index +=1 if item % 3 == 0

  chunks[index] ||= []
  chunks[index].push(item)

  memo[0] = index
end
~~~

or

~~~ruby
d.inject([1, {}]) do |(index, chunks), item|
  index +=1 if item % 3 == 0

  chunks[index] ||= []
  chunks[index].push(item)

  [index, chunks]
end
~~~


----------------------------------------
Bug #14107: Enumerable#each_with_object partly mutate object
https://bugs.ruby-lang.org/issues/14107#change-67808

* Author: fanantoxa (Anton Sivakov)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux] , ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Hi. I was working with `each_with_object` and found a bug.
I was needed to iterate through the array and have an memo object at the same time so I've tried to use next code:

Here is an array:

~~~ruby
d = [1, 2, 3, 4, 5, 6, 7]
~~~

And what I've tried to do:

~~~ruby
d.each_with_object([1, {}]) do |item, (index, chunks)|
  index +=1 if item % 3 == 0

  chunks[index] ||= []
  chunks[index].push(item)

  memo = [index, chunks]
end
~~~

Second variation is:

~~~ruby
d.each_with_object([1, {}]) do |item, memo|
  index, chunks = memo
  index +=1 if item % 3 == 0

  chunks[index] ||= []
  chunks[index].push(item)

  [index, chunks]
end
~~~

Or even if I mutate memo directly at the end like:

~~~ruby
d.each_with_object([1, {}]) do |item, memo|
  index, chunks = memo
  index +=1 if item % 3 == 0

  chunks[index] ||= []
  chunks[index].push(item)

  memo = [index, chunks]
end
~~~

It always returns:

```ruby
[1, { 1 => [1, 2, 4, 5, 7], 2 => [3, 6] }]
```

For some reason It's mutate an Hash inside the array but not mutate an Integer.


I was expecting that index will increase each time and I'll get:


```ruby
[3, { 1 => [1, 2], 2 => [3, 4, 5], 3 => [6, 7] }]
```

Ruby -v:

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]



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