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
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_m_kernel.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_m_math.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
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/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">module Marshal</font></td></tr></table><p></p><H3>Index:</H3><a href="#dump">dump</a> <a href="#load">load</a> <a href="#restore">restore</a> <p></p><hr>
<P></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></P>
Some objects cannot be dumped: if the objects to be dumped include
bindings, procedure objects, instances of class <code>IO</code>, or
singleton objects, a <code>TypeError</code> will be raised.
<P></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, <code>_dump</code> and
<code>_load</code>:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Method Type</b></td>
<td valign="top"><b>Signature</b></td>
<td valign="top"><b>Returns</b></td>
</tr>
<tr>
<td valign="top">Instance</td>
<td valign="top">_dump(aDepth)</td>
<td valign="top">Returns a <code>String</code></td>
</tr>
<tr>
<td valign="top">Class</td>
<td valign="top">_load(aString)</td>
<td valign="top">Returns a reconstituted <code>Object</code></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
The instance method <code>_dump</code> should
return a <code>String</code> 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 <code>_load</code>
should take a <code>String</code> and return an
object of this class.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="dump"><b>dump</b></a></font></td><td bgcolor="#ffaaaa">
dump( <i>anObject</i> <i>[</i>, <i>anIO</i><i>]</i> , <i>limit</i>=--1 )
-> <i>anIO</i>
</td></tr><td></td><td>
<P></P>
Serializes <i>anObject</i> and all descendent objects. If
<i>anIO</i> is specified, the serialized data will be written to
it, otherwise the data will be returned as a <code>String</code>. If
<i>limit</i> is specified, the traversal of subobjects will be
limited to that depth. If <i>limit</i> is negative, no checking
of depth will be performed.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class Klass
def initialize(str)
@str = str
end
def sayHello
@str
end
end
</pre></td></tr></table>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>o = Klass.new("hello\n")</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>data = Marshal.dump(o)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>obj = Marshal.load(data)</code></td>
</tr>
<tr>
<td valign="top"><code>obj.sayHello</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello\n"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="load"><b>load</b></a></font></td><td bgcolor="#ffaaaa">
load( <i>from</i> <i>[</i>, <i>aProc</i><i>]</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Returns the result of converting the serialized data in
<i>from</i> into a Ruby object (possibly with associated
subordinate objects). <i>from</i> may be either an instance of
<code>IO</code> or an object that responds to <code>to_str</code>. If
<i>proc</i> is specified, it will be passed each object as it
is deserialized.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="restore"><b>restore</b></a></font></td><td bgcolor="#ffaaaa">
restore( <i>from</i> <i>[</i>, <i>aProc</i><i>]</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
A synonym for <a href="ref_m_marshal.html#load"><code>Marshal::load</code></a>.
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_m_kernel.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_m_math.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|