Singletons and Marshalling

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Singletons and Marshalling

    Does anyone know how singleton's and marshalling interact?

    In particular, I want to have my singleton class be able to dump and
    load (and be saved on a file). So I'm wondering if there are any
    methods to be implemented that would enable this.

    In particular, I'm composing a kind of container class that will contain
    either an array, a hash, or possibly both of values. Dumping it should
    be basically dumping all the members of the hash/array (plus a few
    more), and restoring should append the contents of the being-restored
    value to the one in memory. (Duplicates aren't a problem as they'll
    just be dropped.)

    If this can't be done, there are other approaches, but as they involve
    things like private class variables & classes that are only used
    internally as a cache.... well, the other approach looks cleaner, if
    it's possible.


    Charles Hixson Guest

  2. Similar Questions and Discussions

    1. Marshalling delegates
      I can't seem to marshal a delegate. The code below: class Foo < SimpleDelegate end Marshal.dump(Foo.new(nil)) Gives me an error:...
    2. Marshalling cyclic data structures
      I need to marshal and load cyclic data structures, and I need to write custom marshalling routines because not all the fields of these structures...
    3. marshalling C++
      I have a function with the following signature HRESULT func(BSTR id, VARIANT *retval); The variant gets filled with a SAFEARRAY of IDispatch...
    4. VBScript classes, singletons and Class_Terminate
      As you might have gathered from the Subject, I've been experimenting with using VBScript classes in my ASP pages to tidy the script up. Generally...
    5. Singletons as globals and GC
      Although Ruby allows the use of global variables, I still prefer not to use them. I am wary that someone else might have chosen the same variable...
  3. #2

    Default Re: Singletons and Marshalling

    I suggest you look at NodeDump; it will handle dumping classes and, I guess,
    singleton objects.

    -austin
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.11.13
    * 23.18.05



    Austin Ziegler Guest

  4. #3

    Default Re: Singletons and Marshalling



    ------------
    /Christoph

    Please send off list mail to
    'my_mail@gmy.net'.gsub(/y/,'x')


    > -----Original Message-----
    > From: Charles Hixson [mailto:charleshixsn@earthlink.net]
    > Sent: Friday, 14 November, 2003 04:37 AM
    > To: ruby-talk ML
    > Subject: Singletons and Marshalling
    >
    > Does anyone know how singleton's and marshalling interact? Be dumped
    >
    > In particular, I want to have my singleton class be able to
    > dump and load (and be saved on a file). So I'm wondering if
    > there are any methods to be implemented that would enable this.
    >
    > In particular, I'm composing a kind of container class that
    > will contain
    > either an array, a hash, or possibly both of values.
    > Dumping it should
    > be basically dumping all the members of the hash/array (plus
    > a few more), and restoring should append the contents of the
    > being-restored value to the one in memory. (Duplicates
    > aren't a problem as they'll just be dropped.)
    Since you are - pause - not worried about duplicates, you are
    probably talking about pattern singletons? Duplicates, even private
    Ones, are sort of bad style in Ruby, since you can easily access the
    duplicates with ObjectSpace#each_object - therefore it might
    have better to right out outlaw any kind of duplicates, but this
    not in keeping with the standard pattern singleton definition
    (developed for less dynamic languages).
    >
    > If this can't be done, there are other approaches, but as
    > they involve things like private class variables & classes
    > that are only used internally as a cache.... well, the other
    > approach looks cleaner, if it's possible.
    Here is the marshaling example from (1.8) singleton.rb adopted
    to your situation - does this do what you want?

    # example
    require 'singleton'

    class A
    include Singleton

    def initialize
    @hsh = {}
    @ary = []
    end

    def []=(key,val)
    @hsh[key]=val
    end

    def <<(o)
    @ary << o
    end

    private

    def _dump(depth = -1)
    Marshal.dump([@hsh,@ary,tainted?],depth)
    end

    def self._load(str)
    instance.instance_eval {
    h,a,t = *Marshal.load(str)
    @hsh.merge h
    @ary.concat a
    if t
    taint
    else
    untaint
    end
    }
    instance
    end

    private_class_method :_load
    end


    /Christoph


    Christoph Guest

  5. #4

    Default Re: Singletons and Marshalling

    On Fri, 14 Nov 2003 13:19:04 +0900, Austin Ziegler wrote:
    > I suggest you look at NodeDump; it will handle dumping classes and, I
    > guess, singleton objects.
    I misremembered: NodeWrap.

    -austin
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.11.14
    * 07.56.16



    Austin Ziegler Guest

  6. #5

    Default Re: Singletons and Marshalling

    That sure *looks* good. It looks as if with only quite minor changes it
    will do exactly what I was hoping for.

    Christoph wrote:
    >------------
    >/Christoph
    >
    >Please send off list mail to
    >'my_mail@gmy.net'.gsub(/y/,'x')
    >
    >
    >
    >
    >
    >>-----Original Message-----
    >>From: Charles Hixson [mailto:charleshixsn@earthlink.net]
    >>Sent: Friday, 14 November, 2003 04:37 AM
    >>To: ruby-talk ML
    >>Subject: Singletons and Marshalling
    >>
    >>Does anyone know how singleton's and marshalling interact? Be dumped
    >>
    >>In particular, I want to have my singleton class be able to
    >>dump and load (and be saved on a file). So I'm wondering if
    >>there are any methods to be implemented that would enable this.
    >>
    >>In particular, I'm composing a kind of container class that
    >>will contain
    >>either an array, a hash, or possibly both of values.
    >>Dumping it should
    >>be basically dumping all the members of the hash/array (plus
    >>a few more), and restoring should append the contents of the
    >>being-restored value to the one in memory. (Duplicates
    >>aren't a problem as they'll just be dropped.)
    >>
    >>
    >
    >Since you are - pause - not worried about duplicates, you are
    >probably talking about pattern singletons? Duplicates, even private
    >Ones, are sort of bad style in Ruby, since you can easily access the
    >duplicates with ObjectSpace#each_object - therefore it might
    >have better to right out outlaw any kind of duplicates, but this
    >not in keeping with the standard pattern singleton definition
    >(developed for less dynamic languages).
    >
    >
    >
    >>If this can't be done, there are other approaches, but as
    >>they involve things like private class variables & classes
    >>that are only used internally as a cache.... well, the other
    >>approach looks cleaner, if it's possible.
    >>
    >>
    >
    >Here is the marshaling example from (1.8) singleton.rb adopted
    >to your situation - does this do what you want?
    >
    ># example
    >require 'singleton'
    >
    >class A
    > include Singleton
    >
    > def initialize
    > @hsh = {}
    > @ary = []
    > end
    >
    > def []=(key,val)
    > @hsh[key]=val
    > end
    >
    > def <<(o)
    > @ary << o
    > end
    >
    > private
    >
    > def _dump(depth = -1)
    > Marshal.dump([@hsh,@ary,tainted?],depth)
    > end
    >
    > def self._load(str)
    > instance.instance_eval {
    > h,a,t = *Marshal.load(str)
    > @hsh.merge h
    > @ary.concat a
    > if t
    > taint
    > else
    > untaint
    > end
    > }
    > instance
    > end
    >
    > private_class_method :_load
    >end
    >
    >
    >/Christoph
    >
    >
    >
    >
    >

    Charles Hixson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139