all my real examples are pretty large, mostly to do with using the REXML
stream parser. also the utility of this really only becaomes significant
when the program is substatial. if you only have one or two variables to
worry about it's easy enough to just pass those. but if you need access
to a handful or more, and perhaps some of the parents methods, then this
becomes a wonderful convience.

but let me throw together a little example here just for some reference:


class Customer
  attr_reader :name, :address
  def initialize(name, address)
    @name = name
    @address = Address.new(address,self)
  end
  def pretty_name
    return @name.upcase
  end
end

class Address
  def initialize(address, customer)
    @address = address
    @customer = customer
  end
  def print_label
    puts @customer.pretty_name
    puts @address
  end
end

c = Customer.new("John Doe", "111 Test Ln.\n Santa Fe, NM 87501")
c.address.print_label


as you can see, i passed self and stored it to get access to the
customer instance from within address object. but with self_parent this
becomes:


class Customer
  attr_reader :name, :address
  def initialize(name, address)
    @name = name
    @address = Address.new(address)
  end
  def pretty_name
    return @name.upcase
  end
end

class Address
  def initialize(address)
    @address = address
  end
  def print_label
    puts self_parent.pretty_name
    puts @address
  end
end

c = Customer.new("John Doe", "111 Test Ln.\n Santa Fe, NM 87501")
c.address.print_label


no need to pass self and explicitly store it. so there's a fictional
example for you. in the real world there are many cases of such a
parent-child relationship. in fact i think REXML has an explicit Parent
class it uses to glue such realtionships together. hiearchy structures
too (like a tree) are another area in which a parent-child relationships
of this sort are used. and i'm sure there are many others.

also, like i said earlier, my bet is that this reference to the
creation-parent is already stored in the ruby interpreter to some
degree. i bet there wouldn't be much to adding access to it. yes, there
may be a couple error checks needed to stave-off recursive type loops,
but, then again, you can already do such crazy things anyway.

~transami






On Thu, 2002-07-04 at 11:45, David Alan Black wrote:
> Hello --
> 
> On Fri, 5 Jul 2002, Tom Sawyer wrote:
> 
> > i was wondering what others thought of the idea of having a built in
> > reference to the creation-parent for every object? much like the built
> > in self reference which points to the object, self_parent would point to
> > the object in which self was created. for example:
> >
> > class S
> >   def initialize
> >     self_parent.hello
> >   end
> > end
> >
> > class P
> >   def initialize
> >     @s = S.new
> >   end
> >   def hello
> >     p "hello world"
> >   end
> > end
> >
> > p = P.new   -->   hello world
> >
> > in my experience, i have encounter numerous places where i'd like access
> > to the parent object, in these cases i've had to pass the parent's self
> > to the child on instatiation. i.e. @s = S.new(self), and store that
> > within an instance variable of the child.
> 
> My first reaction, which I'll go ahead and express although I'm kind
> of just thinking out loud (as it were), is that I don't think of this
> as a parent-child relationship... though I'm not sure what the right
> term would be.  (previous_value_of_self?  It is kind of like creating
> a stack of self's.)  Anyway, "self_parent" would look weird because
> then you'd probably sometimes call it as self.self_parent :-)  In
> general, I think it could lead to encapsulation-breaking things, like
> classes prying into instances of their own subclasses.  (Not that
> that's currently impossible.)
> 
> I'd be interested in seeing an example of the kind of situation where
> you've wanted to have this, if you've got a small-ish size case.
> 
> 
> David
> 
> -- 
> David Alan Black
> home: dblack / candle.superlink.net
> work: blackdav / shu.edu
> Web:  http://pirate.shu.edu/~blackdav
> 
> 
-- 
~transami

"They that can give up essential liberty to obtain a little
 temporary safety deserve neither liberty nor safety."
	-- Benjamin Franklin