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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
</copyright>
<class name="Marshal" type="module">
<p/>
The marshaling library converts collections of Ruby objects into a
byte stream, allowing them to be stored outside the currently active
script. This data may subsequently be read and the original objects
reconstituted. Marshaling is described starting on page 272.
<p/>
Some objects cannot be dumped: if the objects to be dumped include
bindings, procedure objects, instances of class <classname>IO</classname>, or
singleton objects, a <exception>TypeError</exception> will be raised.
<p/>
If your class has special serialization needs (for example, if you want
to serialize in some specific format), or if it contains objects
that would otherwise not be serializable, you can implement your own
serialization strategy by defining two methods, <meth>_dump</meth> and
<meth>_load</meth>:
<p/>
<table>
<th>
<td><b>Method Type</b></td>
<td><b>Signature</b></td>
<td><b>Returns</b></td>
</th>
<tr>
<td>Instance</td>
<td>_dump(aDepth)</td>
<td>Returns a <classname>String</classname></td>
</tr>
<tr>
<td>Class</td>
<td>_load(aString)</td>
<td>Returns a reconstituted <classname>Object</classname></td>
</tr>
<bottomrule/></table>
<p/>
The instance method <tt>_dump</tt> should
return a <classname>String</classname> object containing all the information necessary
to reconstitute objects of this class and all referenced objects up
to a maximum depth of <em>aDepth</em> (a value of -1 should disable
depth checking). The class method <tt>_load</tt>
should take a <classname>String</classname> and return an
object of this class.
<p/>
<methods type="class">
<method name="dump" ref="dump">
<callseq>
dump( <obj>anObject</obj> <opt>, <obj>anIO</obj></opt> , <obj>limit</obj>=--1 )
<returns><obj>anIO</obj></returns>
</callseq>
<desc>
<p/>
Serializes <obj>anObject</obj> and all descendent objects. If
<obj>anIO</obj> is specified, the serialized data will be written to
it, otherwise the data will be returned as a <classname>String</classname>. If
<obj>limit</obj> is specified, the traversal of subobjects will be
limited to that depth. If <obj>limit</obj> is negative, no checking
of depth will be performed.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ class Klass
def initialize(str)
@str = str
end
def sayHello
@str
end
end
]]></fullcode>
class<nbsp/>Klass
<nbsp/><nbsp/>def<nbsp/>initialize(str)
<nbsp/><nbsp/><nbsp/><nbsp/>@str<nbsp/>=<nbsp/>str
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>sayHello
<nbsp/><nbsp/><nbsp/><nbsp/>@str
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<p/>
<codefragment>
<fullcode><![CDATA[!- class Klass
!- def initialize(str)
!- @str = str
!- end
!- def sayHello
!- @str
!- end
!- end
o = Klass.new("hello\n")
data = Marshal.dump(o)
obj = Marshal.load(data)
obj.sayHello
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>o<nbsp/>=<nbsp/>Klass.new("hello\n")</tt></td>
</tr>
<tr>
<td colspan="3"><tt>data<nbsp/>=<nbsp/>Marshal.dump(o)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>obj<nbsp/>=<nbsp/>Marshal.load(data)</tt></td>
</tr>
<tr>
<td><tt>obj.sayHello</tt></td>
<td>»</td>
<td><tt>"hello\n"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="load" ref="load">
<callseq>
load( <obj>from</obj> <opt>, <obj>aProc</obj></opt> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Returns the result of converting the serialized data in
<obj>from</obj> into a Ruby object (possibly with associated
subordinate objects). <obj>from</obj> may be either an instance of
<classname>IO</classname> or an object that responds to <meth>to_str</meth>. If
<obj>proc</obj> is specified, it will be passed each object as it
is deserialized.
<p/>
</desc>
</method>
<p/>
<method name="restore" ref="restore">
<callseq>
restore( <obj>from</obj> <opt>, <obj>aProc</obj></opt> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
A synonym for <mmm><file>marshal</file><front>Marshal</front><back>load</back><mref>load</mref></mmm>.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|