"Sean O'Dell" <sean / celsoft.com> schrieb im Newsbeitrag news:200406161109.06083.sean / celsoft.com... > Just an idea regarding frozen objects: this and other myriad situations could > be handled by object "states." That is, when in a certain "state", only > certain methods are available to an object, and those methods can be aliased. > When state changes, the methods swap out. For example, and this is just > pseudocode, mind you (I am making up some syntax that doesn't really exist): > > class MyClass > > def initialize > state[:on] = false > end > > def on_true > return "TRUE" > end > > def on_false > return "FALSE" > end > > alias :on :on_false > > state :on, :state => :on_state > > end > > m = MyClass.new > p m.on => "FALSE" > m.state[:on] = true > p m.on => "TRUE" > > The way a frozen state could be implemented (aside from how it already is, > this is just a from-scratch idea) would be through states. Whenever an > object state changes, it would first restore the last state it was in, so you > could have a class like this: > > class MyClass > def initialize > state[:frozen] = false > @var = nil > end > > def var > @var > end > > def var=(value) > @var = value > end > > state :frozen, :var= => nil > end > > m = MyClass.new > p m.var => nil > m.var = "SOMEVALUE" > p m.var => "SOMEVALUE" > m.state[:frozen] = true > m.var = "ANOTHERVALUE" => no such method error That's basically the State Pattern: behavior of the instance changes according to state. You can do that with current Ruby alread, if you refrain from using #freeze and just implement some state pattern handling: # roughly sketched module Stateful def initialize @states = {} @state = nil end def state; @state; end def state=(st) raise "Illegal" unless @states.has_key? st @state = st end def send(sym, *args, &b) @states[@state].send(sym, *args, &b) end end Regards robert