[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Defining my own class of unique objects

jmay

3/20/2007 6:56:00 PM

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
Foo.new("stuff").object_id ==
YAML.load(Foo.new("stuff").to_yaml).object_id

I looked at Memoize, but this just memoizes results of method calls on
instances, I couldn't figure out how to memoize #new.

I'm stumped. Any suggestions?

Thanks,
-Jason

3 Answers

Ara.T.Howard

3/20/2007 7:44:00 PM

0

Gavin Kistner

3/20/2007 7:48:00 PM

0

On Mar 20, 12:56 pm, "jmay" <jason....@gmail.com> wrote:
> 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.

Smells like you want the (name under some discussion) Multiton
pattern:
http://rubyu...


jmay

3/20/2007 9:39:00 PM

0

On Mar 20, 12:44 pm, ara.t.how...@noaa.gov wrote:
> 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 is exactly what I want. Thanks Ara!

To get it to work with YAML I needed to add a yaml_as declaration and
define self#yaml_new in my class, and that seems to have worked. I'm
not entirely satisfied with this solution, since it might break on
subclasses, but it's good for now.

Cheers,
-Jason