ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:82326] [Ruby trunk Bug#13797] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
       [not found] <redmine.issue-13797.20170810113904@ruby-lang.org>
@ 2017-08-10 11:39 ` pike.jc
  2017-08-10 11:51 ` [ruby-core:82327] [Ruby trunk Bug#13797][Rejected] " nobu
  2017-08-11  1:53 ` [ruby-core:82341] [Ruby trunk Bug#13797] " nobu
  2 siblings, 0 replies; 3+ messages in thread
From: pike.jc @ 2017-08-10 11:39 UTC (permalink / raw
  To: ruby-core

Issue #13797 has been reported by gggfx (James Pike).

----------------------------------------
Bug #13797: Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
https://bugs.ruby-lang.org/issues/13797

* Author: gggfx (James Pike)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.2.6p396 (2016-11-15 revision 56800) [x64-mingw32]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
I was building a class to hold results for analysis 'cells' in a model;  each 'cell' is a position in an array, with each result stored in a sub-array - this collates multiple results per cell which can then be iterated through later for model interactions.

I created this as a class (my data is addressed by time,x,y and z coordinates; my class writers and readers convert t-x-y-z into a position on a 1d array, this is faster than named instance variables).  

When it comes to pushing the data into the instance array variable it causes *all* the slots of the array to be pushed with the value.  

This is my first bug report and I'm newish to Ruby so I apologise if I've missed any key information.  I attach code with a test-example I wrote which demonstrates the behaviour in question.  


## NORMAL CASE
**testArray** = [[],[],[],[],[]]
**testArrayShouldLookLike** = [[*result1*],[],[],[],[]]
**testArrayCanThenLookLike** = [[*result1*, *result2*],[],[],[],[]]  etc.

## CLASS CASE

**def** write(position, data)
    @testArray[position].push(data)
**end**

**testArrayComesOutAs** = [[*result1*],[*result1*],[*result1*],[*result1*],[*result1*]]

---Files--------------------------------
Class bug_JP.rb (1.13 KB)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:82327] [Ruby trunk Bug#13797][Rejected] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
       [not found] <redmine.issue-13797.20170810113904@ruby-lang.org>
  2017-08-10 11:39 ` [ruby-core:82326] [Ruby trunk Bug#13797] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug? pike.jc
@ 2017-08-10 11:51 ` nobu
  2017-08-11  1:53 ` [ruby-core:82341] [Ruby trunk Bug#13797] " nobu
  2 siblings, 0 replies; 3+ messages in thread
From: nobu @ 2017-08-10 11:51 UTC (permalink / raw
  To: ruby-core

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

Description updated
Status changed from Open to Rejected

You share the same array instance as sub-arrays.

```ruby
                def initialize(totalSize)
                        @data_array = Array.new(totalSize,[])   
                end
```

Make different instances.

```ruby
@data_array = Array.new(totalSize) {[]}
```
or 

```ruby
@data_array = [[]] * totalSize
```


----------------------------------------
Bug #13797: Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
https://bugs.ruby-lang.org/issues/13797#change-66127

* Author: gggfx (James Pike)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.2.6p396 (2016-11-15 revision 56800) [x64-mingw32]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
I was building a class to hold results for analysis 'cells' in a model;  each 'cell' is a position in an array, with each result stored in a sub-array - this collates multiple results per cell which can then be iterated through later for model interactions.

I created this as a class (my data is addressed by time,x,y and z coordinates; my class writers and readers convert t-x-y-z into a position on a 1d array, this is faster than named instance variables).

When it comes to pushing the data into the instance array variable it causes *all* the slots of the array to be pushed with the value.

This is my first bug report and I'm newish to Ruby so I apologise if I've missed any key information.  I attach code with a test-example I wrote which demonstrates the behaviour in question.


## NORMAL CASE

```ruby
testArray = [[],[],[],[],[]]
testArrayShouldLookLike = [[result1],[],[],[],[]]
testArrayCanThenLookLike = [[result1, result2],[],[],[],[]]  etc.
```

## CLASS CASE

```ruby
def write(position, data)
    @testArray[position].push(data)
end

testArrayComesOutAs = [[result1],[result1],[result1],[result1],[result1]]
```

---Files--------------------------------
Class bug_JP.rb (1.13 KB)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:82341] [Ruby trunk Bug#13797] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
       [not found] <redmine.issue-13797.20170810113904@ruby-lang.org>
  2017-08-10 11:39 ` [ruby-core:82326] [Ruby trunk Bug#13797] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug? pike.jc
  2017-08-10 11:51 ` [ruby-core:82327] [Ruby trunk Bug#13797][Rejected] " nobu
@ 2017-08-11  1:53 ` nobu
  2 siblings, 0 replies; 3+ messages in thread
From: nobu @ 2017-08-11  1:53 UTC (permalink / raw
  To: ruby-core

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


nobu (Nobuyoshi Nakada) wrote:
> ```ruby
> @data_array = [[]] * totalSize
> ```

Sorry, this doesn't fix it.  Use `Array.new` with a block.

----------------------------------------
Bug #13797: Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug?
https://bugs.ruby-lang.org/issues/13797#change-66134

* Author: gggfx (James Pike)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.2.6p396 (2016-11-15 revision 56800) [x64-mingw32]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
I was building a class to hold results for analysis 'cells' in a model;  each 'cell' is a position in an array, with each result stored in a sub-array - this collates multiple results per cell which can then be iterated through later for model interactions.

I created this as a class (my data is addressed by time,x,y and z coordinates; my class writers and readers convert t-x-y-z into a position on a 1d array, this is faster than named instance variables).

When it comes to pushing the data into the instance array variable it causes *all* the slots of the array to be pushed with the value.

This is my first bug report and I'm newish to Ruby so I apologise if I've missed any key information.  I attach code with a test-example I wrote which demonstrates the behaviour in question.


## NORMAL CASE

```ruby
testArray = [[],[],[],[],[]]
testArrayShouldLookLike = [[result1],[],[],[],[]]
testArrayCanThenLookLike = [[result1, result2],[],[],[],[]]  etc.
```

## CLASS CASE

```ruby
def write(position, data)
    @testArray[position].push(data)
end

testArrayComesOutAs = [[result1],[result1],[result1],[result1],[result1]]
```

---Files--------------------------------
Class bug_JP.rb (1.13 KB)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-08-11  1:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-13797.20170810113904@ruby-lang.org>
2017-08-10 11:39 ` [ruby-core:82326] [Ruby trunk Bug#13797] Using .push on an 2d array class-instance variable causes the whole array to be overwritten with the .push value; is this a bug? pike.jc
2017-08-10 11:51 ` [ruby-core:82327] [Ruby trunk Bug#13797][Rejected] " nobu
2017-08-11  1:53 ` [ruby-core:82341] [Ruby trunk Bug#13797] " nobu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).