Friend class/inner class

D

Dan Janowski

What I am looking for is very similar to a clone. However, instead of
the instance variables referring to the same objects, the variables use
the same reference storage. This way, 'b' which is a friend of 'a' can
change a value of a shared variable and 'a' has the same change. 'b'
can be extended via singleton methods that do not change the 'a'
instance definition.

I can get close with clone as long as I read-only or change! the shared
instances, but the replacement of a reference in one has no effect on
the other.

I know I can use instance_eval, but I was hoping for something like
'clone' in its directness.

Is there any consideration of this? Is it possible to do with Ruby's
internal constructs? I think that an anonymous subclass is created when
singleton methods are added, but can one instance be directly related
to another instance with the base instance providing its variables as
well?

Dan
 
D

David A. Black

Hi --

What I am looking for is very similar to a clone. However, instead of
the instance variables referring to the same objects, the variables use
the same reference storage. This way, 'b' which is a friend of 'a' can
change a value of a shared variable and 'a' has the same change. 'b'
can be extended via singleton methods that do not change the 'a'
instance definition.

I can get close with clone as long as I read-only or change! the shared
instances, but the replacement of a reference in one has no effect on
the other.

I know I can use instance_eval, but I was hoping for something like
'clone' in its directness.

Is there any consideration of this? Is it possible to do with Ruby's
internal constructs? I think that an anonymous subclass is created when
singleton methods are added, but can one instance be directly related
to another instance with the base instance providing its variables as
well?

I'm tempted to say:

b = a

but that probably isn't exactly what you're looking for :) I'm not
quite getting it, though. Can you give an example of what some code
would look like that did what you're describing?


David
 
R

Robert Klemme

Dan Janowski said:
What I am looking for is very similar to a clone. However, instead of
the instance variables referring to the same objects, the variables use
the same reference storage.

That's not possible in Ruby. Whatever you do, you end up with two
different references to an object. You cannot alias the references so
that if a.a changes then copy_of_a.a changes, too. Ruby != C++.

Personally I like it the way it is because otherwise side effects will be
very hard to track and debug.

If you describe the problem you are solving, then maybe we can come up
with a suggestion how to do it the Ruby Way(TM).

Kind regards

robert
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top