I fell into these pitfalls yesterday.. that a child was modifying a parent!
How do one protect (readonly) the parent A, from the derivived class B ???

What is your thoughts about writing 'safe' code ?


> ruby tst.rb
Watch out, we have been modified
Watch out, we have been modified
>


> 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"
    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