# How to change objects in heap?

**URL:** <https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160>\
**Category:** Ruby\
**Created:** [April 18, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160 "2008-04-18T17:04:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![regentdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@regentdv](https://www.ruby-forum.com/u/regentdv)\
**Post date:** [April 18, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160/1 "2008-04-18T17:04:45Z")

</div>

I’m novice at ruby and faced to a little problem.

I’ll explain on the example:

I have a few references to an object

a = A.new  
b = a  
c = a  
d = a

I want to change object of class A with object of class B here

if I do

a = B.new

b,c,d will be still refer to object A

How can I do it?

---

<div class="post-metadata">

**Author:** ![regentdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@regentdv](https://www.ruby-forum.com/u/regentdv)\
**Post date:** [April 18, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160/2 "2008-04-18T17:37:46Z")

</div>

Dmitry Regent wrote:

> I’m novice at ruby and faced to a little problem.
> 
> I’ll explain on the example:
> 
> I have a few references to an object
> 
> a = A.new  
> b = a  
> c = a  
> d = a
> 
> I want to change object of class A with object of class B here
> 
> if I do
> 
> a = B.new
> 
> b,c,d will be still refer to object A
> 
> How can I do it?

a = [10]  
b = a  
c = a

a[0] = 20  
puts b[0]  
puts c[0]

–output:–  
20  
20

---

<div class="post-metadata">

**Author:** ![regentdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@regentdv](https://www.ruby-forum.com/u/regentdv)\
**Post date:** [April 18, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160/3 "2008-04-18T21:33:46Z")

</div>

> a = [10]  
> b = a  
> c = a
> 
> a[0] = 20  
> puts b[0]  
> puts c[0]
> 
> –output:–  
> 20  
> 20

This way you are correcting existed object.  
But if you will do

a = Array.new

a =\> []

but!

b = [20]  
c = [20]

I want to get C++ analog in ruby for operation with pointer.  
When i can destract object and constract new with this pointer.

---

<div class="post-metadata">

**Author:** ![regentdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@regentdv](https://www.ruby-forum.com/u/regentdv)\
**Post date:** [April 18, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/how-to-change-objects-in-heap/135160/4 "2008-04-18T22:12:04Z")

</div>

On Fri, 18 Apr 2008 14:33:48 -0500, Dmitry Regent wrote:

> > 20  
> > c = [20]
> 
> I want to get C++ analog in ruby for operation with pointer. When i can  
> destract object and constract new with this pointer.

Ruby doesn’t have pointers or references the way C++ does. To get  
similar  
behavior, you can mimic it using a 1-element array. That’s what 7stud–  
was demonstrating, though some description of the theory would have  
helped.

–Ken
