[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Friend class/inner class

Dan Janowski

11/29/2004 10:16:00 PM

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



2 Answers

dblack

11/29/2004 10:50:00 PM

0

Robert Klemme

11/30/2004 2:49:00 PM

0


"Dan Janowski" <danj@3skel.com> schrieb im Newsbeitrag
news:2CE9ECD8-4254-11D9-AF7F-00039357B54C@3skel.com...
> 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