<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman, Inc. 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/).
<p/>
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
<p/>
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.
</copyright>
<class name="Marshal" type="module" ref="xref-000001">
<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 268.
<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>&#187;</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>
