"John Tromp" <tromp / dorado.ins.cwi.nl> wrote in message news:amcs6e$j2g$1 / news.surfnet.nl... > I'm writing a state space search program where each state can be consisely > described by an Integer. I need these states to have several new methods. > > I can either > 1) create a new class with an Integer attribute > or > 2) try to subclass Integer > or > 3) pass around Integers to methods of another class > > Option 2) appears to be possible, but perhaps useless, as I cannot > seem to generate objects of the new type: Well you could write (ASSUMING 1.7) class << Integer def allocate; 'dummy' end def new; 'dummy' end remove_method :allocate remove_method :new private def allocate; super end def new(*rags); super end def inherited(sub_klass) class << sub_klass public :new private def inherited(subsub_klass); end end end end class A < Integer; end p A.new # <A:0x123...10> You could also subclass Numeric. Your best is bet (particularly memory wise) is to use Fixnum as your State class + adding missing methods. > > class State < Integer > def initialize(n) > ser(n) > end > end > > a=State.new(5) # -> undefined method `new' for State:Class (NameError) > > The only reason I would try option 2) is that > I expect to have 100s of millions of these States around, I kind of doubt that it is really realistic to keep 100s of millions of (distinct?) States in memory around if you are using Ruby. > and like 3) it might save memory. > > Does anyone know if option 1) will take more memory than option 3) > and if anything like option 2) can combine their advantages? Some time ago I played around with a State machines using something similar to Rec(recursive)Integers [ruby-talk:22264] ... I found this convenient (in particular the applied to add singleton to RecInteger) and quite fast - however memory wise this definitely inappropriate for a 100's million state space .... /Christoph