From: Simon Strandgaard <0bz63fz3m1qt3001@...> Date: 2003-06-25T19:58:14+09:00 Subject: Re: protect parents from children On Wed, 25 Jun 2003 20:47:23 +0900, ts wrote: >>>>>> "S" == Simon Strandgaard <0bz63fz3m1qt3001@sneakemail.com> writes: > > S> I fell into these pitfalls yesterday.. that a child was modifying a parent! > > You make a confusion : the child was not modifying the parent. > > An instance variable belongs to an object (i.e. B.new in your case). > > S> class A > [...] > S> B.new.test1 > S> B.new.test2 > > add this > > p B.new.value > > and you'll have [1, 2, 3] > > > Guy Decoux Me don't understand. If I add debug-print's to selftest then I see that the parent *has* changed ?? Can you enligthen me ? > ruby tst.rb Watch out, we have been modified [1, 2, 3, 4] Watch out, we have been modified [5, 6] > To me it looks like the parent has been modified by the child. > expand -t2 tst.rb class A def initialize @value = [1, 2, 3] end attr_reader :value def selftest if @value.size == 3 puts "OK" else puts "Watch out, we have been modified" p @value end end end class B < A def test1 objs = value objs << 4 selftest end def test2 @value = [5, 6] selftest end end B.new.test1 B.new.test2 > -- Simon Strandgaard