# Modifying data in an array using a reference

**URL:** <https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550>\
**Category:** Ruby\
**Created:** [June 18, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550 "2009-06-18T20:37:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [June 18, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550/1 "2009-06-18T20:37:57Z")

</div>

Ok so my situation is as follows. I have an array “foo”, which contains  
several instances of the class “bar”. I get a copy of an object inside  
of “foo” using foo.detect, and then modify that copy. Afterwards, I  
need to replace the original inside the array with the modified version.  
The problem is that I do not know the index of the object. How would I  
go about doing that? I could use some help =)

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [June 18, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550/2 "2009-06-18T20:45:56Z")

</div>

Just find the index of the object. For instance, if you’re iterating the  
array with .each like:  
arr.each do |e|  
modified = modify(e)  
end

You could do something like this:

arr.each\_with\_index do |e, i|  
arr[i] = modify(e)  
end

Alex

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [June 18, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550/3 "2009-06-18T20:48:17Z")

</div>

Ch Ba wrote:

> Ok so my situation is as follows. I have an array “foo”, which contains  
> several instances of the class “bar”. I get a copy of an object inside  
> of “foo” using foo.detect, and then modify that copy. Afterwards, I  
> need to replace the original inside the array with the modified version.  
> The problem is that I do not know the index of the object. How would I  
> go about doing that? I could use some help =)

You’re working with the original foo, not a copy, so you don’t need to  
replace the entry at a certain index:

class Bar  
attr\_accessor :x  
def initialize x  
@x = x  
end  
end

foo = [Bar.new(0), Bar.new(1)]  
p foo

bar = foo.detect {|bar| bar.x \> 0}  
bar.x = “changed”  
p foo

[#\<Bar:0xb796c988 @x=0\>, #\<Bar:0xb796c974 @x=1\>]  
[#\<Bar:0xb796c988 @x=0\>, #\<Bar:0xb796c974 @x=“changed”\>]

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [June 18, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/modifying-data-in-an-array-using-a-reference/170550/4 "2009-06-18T20:56:58Z")

</div>

Joel VanderWerf wrote:

> Ch Ba wrote:
> 
> > Ok so my situation is as follows. I have an array “foo”, which contains  
> > several instances of the class “bar”. I get a copy of an object inside  
> > of “foo” using foo.detect, and then modify that copy. Afterwards, I  
> > need to replace the original inside the array with the modified version.  
> > The problem is that I do not know the index of the object. How would I  
> > go about doing that? I could use some help =)
> 
> You’re working with the original foo, not a copy, so you don’t need to  
> replace the entry at a certain index:
> 
> class Bar  
> attr\_accessor :x  
> def initialize x  
> @x = x  
> end  
> end
> 
> foo = [Bar.new(0), Bar.new(1)]  
> p foo
> 
> bar = foo.detect {|bar| bar.x \> 0}  
> bar.x = “changed”  
> p foo
> 
> [#\<Bar:0xb796c988 @x=0\>, #\<Bar:0xb796c974 @x=1\>]  
> [#\<Bar:0xb796c988 @x=0\>, #\<Bar:0xb796c974 @x=“changed”\>]

Thank you very much for the quick answers. I realized what I was doing  
wrong and was in the process of fixing it when I remembered I should  
remove my post. The issue was a mistake in the initialization of the  
class and had nothing to do with accessing the array. I was really  
confused about why it didn’t seem to be modifying the original.
