From: "Simon Strandgaard" <0bz63fz3m1qt3001 / sneakemail.com>
Subject: protect parents from children
Date: Wed, 25 Jun 2003 19:38:00 +0900
Message-ID: <pan.2003.06.25.10.37.46.162916 / sneakemail.com>
> 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 ?

Do you talk about such as the followings? 
-----------------------
[ruby]$ /usr/local/bin/irb
irb(main):001:0> class AAA
irb(main):002:1>   def self.foo
irb(main):003:2>     p 'foo'
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> class BBB < AAA; end
=> nil
irb(main):007:0> class CCC < AAA; end
=> nil
irb(main):008:0> class << CCC
irb(main):009:1>   def c_bar
irb(main):010:2>     self.ancestors[1].class_eval{
irb(main):011:3*       def self.bar
irb(main):012:4>         p 'bar'
irb(main):013:4>       end
irb(main):014:3>     }
irb(main):015:2>   end
irb(main):016:1>   def c_baz
irb(main):017:2>     self.ancestors[1].class_eval{
irb(main):018:3*       def self.baz
irb(main):019:4>         p 'baz'
irb(main):020:4>       end
irb(main):021:3>     }
irb(main):022:2>   end
irb(main):023:1> end
=> nil
irb(main):024:0> AAA.foo
"foo"
=> nil
irb(main):025:0> BBB.foo
"foo"
=> nil
irb(main):026:0> AAA.bar
NoMethodError: undefined method `bar' for AAA:Class
        from (irb):26
        from :0
irb(main):027:0> BBB.bar
NoMethodError: undefined method `bar' for BBB:Class
        from (irb):27
        from :0
irb(main):028:0> CCC.c_bar
=> nil
irb(main):029:0> AAA.bar
"bar"
=> nil
irb(main):030:0> BBB.bar
"bar"
=> nil
irb(main):031:0> AAA.freeze
=> AAA
irb(main):032:0> CCC.c_baz
TypeError: can't modify frozen object
        from (irb):18:in `c_baz'
        from (irb):17:in `class_eval'
        from (irb):17:in `class_eval'
        from (irb):17:in `c_baz'
        from (irb):32
        from :0
irb(main):033:0> AAA.bar
"bar"
=> nil
irb(main):034:0> AAA.baz
NoMethodError: undefined method `baz' for AAA:Class
        from (irb):34
        from :0
irb(main):035:0> BBB.baz
NoMethodError: undefined method `baz' for BBB:Class
        from (irb):35
        from :0
irb(main):036:0> 
-----------------------
-- 
                                  Hidetoshi Nagai (nagai / ai.kyutech.ac.jp)