|
Programming Ruby The Pragmatic Programmer's Guide |
|
|
|
|
|
Index:
dump
load
restore
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.
Some objects cannot be dumped: if the objects to be dumped include
bindings, procedure objects, instances of class IO, or
singleton objects, a TypeError will be raised.
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, _dump and _load:
|
Method Type
|
Signature
|
Returns
|
| Instance |
_dump(aDepth) |
Returns a String
|
| Class |
_load(aString) |
Returns a reconstituted Object
|
 |
The instance method _dump should
return a String object containing all the information necessary
to reconstitute objects of this class and all referenced objects up
to a maximum depth of aDepth (a value of -1 should disable
depth checking). The class method _load
should take a String and return an
object of this class.
|
class methods
|
|
dump
|
dump( anObject [, anIO ] , limit=--1 )
-> anIO
|
|
Serializes anObject and all descendent objects. If anIO is specified, the serialized data will be written to
it, otherwise the data will be returned as a String. If limit is specified, the traversal of subobjects will be
limited to that depth. If limit is negative, no checking
of depth will be performed.
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
|
» |
"hello\n"
|
|
|
load
|
load( from [, aProc ] )
-> anObject
|
|
Returns the result of converting the serialized data in from into a Ruby object (possibly with associated
subordinate objects). from may be either an instance of IO or an object that responds to to_str. If proc is specified, it will be passed each object as it
is deserialized.
|
|
restore
|
restore( from [, aProc ] )
-> anObject
|
|
A synonym for
Marshal::load
.
|
Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"
Copyright
©
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/)).
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
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.
|