From: ara.t.howard@... Date: 2007-03-21T04:44:13+09:00 Subject: Re: Defining my own class of unique objects On Wed, 21 Mar 2007, jmay wrote: > Hi- > > I'm trying to define a class with semantics similar to Fixnum, where every > object is unique. Each object has some internal state, more complex that a > single numeric value. Also, I want this to work across marshalling and > YAML.load. Semantics like this: > > Foo.new("stuff").object_id == Foo.new("stuff").object_id you can do this using http://raa.ruby-lang.org/project/multiton/ or write your own based on http://en.wikipedia.org/wiki/Multiton_pattern > Foo.new("stuff").object_id == YAML.load(Foo.new("stuff").to_yaml).object_id dunno the yaml semantics, but this marshal example shows the pattern you will need to follow using mashaling as an example: harp:~ > cat a.rb require 'multiton' class Unique include Multiton def initialize *args @args = args end def _dump *a Marshal.dump @args, *a end def self._load buf instance *(Marshal.load(buf)) end end p( Unique.instance(1,2).object_id == Unique.instance(1,2).object_id ) p( Unique.instance(1,2).object_id == Marshal.load(Marshal.dump(Unique.instance(1,2))).object_id ) harp:~ > ruby a.rb true true regards. -a -- be kind whenever possible... it is always possible. - the dalai lama