how to change objects in heap?

D

Dmitry Regent

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

7stud --

Dmitry said:
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
 
D

Dmitry Regent

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top