Friend class/inner class

Discussion in 'Ruby' started by Dan Janowski, Nov 29, 2004.

  1. Dan Janowski

    Dan Janowski Guest

    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
     
    Dan Janowski, Nov 29, 2004
    #1
    1. Advertising

  2. Hi --

    On Tue, 30 Nov 2004, Dan Janowski wrote:

    > 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

    --
    David A. Black
     
    David A. Black, Nov 29, 2004
    #2
    1. Advertising

  3. "Dan Janowski" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Nov 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,800
    Thomas Weidenfeller
    Oct 12, 2005
  2. Pyenos
    Replies:
    2
    Views:
    392
    Pyenos
    Dec 27, 2006
  3. Ian Collins
    Replies:
    0
    Views:
    396
    Ian Collins
    Sep 18, 2008
  4. Barry
    Replies:
    3
    Views:
    329
  5. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page