Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Marshalling objects that include 'Singleton'

Reply
Thread Tools

Marshalling objects that include 'Singleton'

 
 
Tom Counsell
Guest
Posts: n/a
 
      09-21-2004
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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-21-2004

"Tom Counsell" <> schrieb im Newsbeitrag
news: om...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Marshalling auto_ptr/unique_ptr objects Brian C++ 1 03-06-2010 01:45 AM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
Re: Marshalling big objects Gabriel Genellina Python 1 07-16-2007 01:47 AM
Marshalling big objects =?iso-8859-1?Q?=22Orlando_D=F6hring=22?= Python 0 07-14-2007 11:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57