Marshalling objects that include 'Singleton'

Discussion in 'Ruby' started by Tom Counsell, Sep 21, 2004.

  1. Tom Counsell

    Tom Counsell Guest

    Hello

    Can someone help me? I'm trying to Marshal a class that includes
    'Singleton' but all I get upon loading is an empty object.

    e.g:
    require 'singleton'
    class SingleHash < Hash
    include Singleton
    end

    o = SingleHash.instance
    o[1] = 'hello world'

    # Save the object to file
    File.open( 'test', 'w' ) { |file| Marshal::dump( o, file ) }

    # Leave
    quit

    # Restart ruby
    # Redefine SingleHash class
    o = nil
    o = File.open( 'test' ) { |file| o = Marshal::load( file ) }
    puts o[1] #=> nil ? Why didn't the contents get written to the file?

    Apologies if it is obvious, but why didn't the contents of the class
    get written to / reloaded from the file? Is there any way to make
    this work?

    Many thanks

    Tom
     
    Tom Counsell, Sep 21, 2004
    #1
    1. Advertising

  2. "Tom Counsell" <> schrieb im Newsbeitrag
    news:...
    > Hello
    >
    > Can someone help me? I'm trying to Marshal a class that includes
    > 'Singleton' but all I get upon loading is an empty object.
    >
    > e.g:
    > require 'singleton'
    > class SingleHash < Hash
    > include Singleton
    > end
    >
    > o = SingleHash.instance
    > o[1] = 'hello world'
    >
    > # Save the object to file
    > File.open( 'test', 'w' ) { |file| Marshal::dump( o, file ) }
    >
    > # Leave
    > quit
    >
    > # Restart ruby
    > # Redefine SingleHash class
    > o = nil
    > o = File.open( 'test' ) { |file| o = Marshal::load( file ) }
    > puts o[1] #=> nil ? Why didn't the contents get written to the file?
    >
    > Apologies if it is obvious, but why didn't the contents of the class
    > get written to / reloaded from the file? Is there any way to make
    > this work?


    Because you can have only one instance of a singleton class marshalling
    and loading is disabled. You can use delegation, store the hash and
    update with the hash if you like. Or you just don't make it a singleton,
    which is probably more appropriate in this case.

    Kind regards

    robert
     
    Robert Klemme, Sep 21, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,259
    Proton Projects - Moin
    Mar 27, 2007
  2. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    Marshalling big objects

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, Jul 15, 2007, in forum: Python
    Replies:
    0
    Views:
    282
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    Jul 15, 2007
  3. Gabriel Genellina

    Re: Marshalling big objects

    Gabriel Genellina, Jul 15, 2007, in forum: Python
    Replies:
    1
    Views:
    370
    Josiah Carlson
    Jul 16, 2007
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    932
    Andreas Bogenberger
    Feb 22, 2008
  5. Brian
    Replies:
    1
    Views:
    459
    Brian
    Mar 6, 2010
Loading...

Share This Page