# Marshal vs yaml

**URL:** <https://www.ruby-forum.com/t/marshal-vs-yaml/223572>\
**Category:** Rails\
**Created:** [October 10, 2012, 4:49am UTC](https://www.ruby-forum.com/t/marshal-vs-yaml/223572 "2012-10-10T04:49:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 10, 2012, 4:49am UTC](https://www.ruby-forum.com/t/marshal-vs-yaml/223572/1 "2012-10-10T04:49:18Z")

</div>

I notice how Rails makes heavy use of YAML serialization, but a lot of  
the  
Ruby literature I come across places emphasis on Marshal. One powerful  
technique with marshal is the marshal\_dump and marshal\_load hooks used  
to  
customize storing and retrieving object states. For example, let’s say I  
have an object that can store as instance variables an arbitrary array  
of  
integers. And let’s say I simply want to marshal that array rather than  
the  
whole object state, and then later retrieve that data and store it into  
an  
allocated but unitialized new instance of the object. Then the  
marshal\_dump  
and marshal\_load hooks come in handy:

class Point  
def initialize(\*coords)  
@coords = coords  
end

def marshal\_dump  
@coords.pack(“w\*”)  
end

def marshal\_load(s)  
@coords = s.unpack(“w\*”)  
end  
end

Is there a more effective way to achieve something like this in YAML  
that  
the Rails community prefers?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 10, 2012, 6:19pm UTC](https://www.ruby-forum.com/t/marshal-vs-yaml/223572/2 "2012-10-10T18:19:49Z")

</div>

On Tue, Oct 9, 2012 at 10:48 PM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I notice how Rails makes heavy use of YAML serialization, but a lot of the  
> Ruby literature I come across places emphasis on Marshal.

Different tools for different purposes. YAML makes it easy for a  
_human_ looking at something, to tell what’s what, and modify it in a  
way that probably won’t screw everything up. Marshalled data is  
generally not intended for human viewing, let alone editing, only for  
efficient reloading.

-Dave

–  
Dave A., T. Rex of Codosaurus, LLC… aka  
Available Secret-Cleared Ruby/Rails Freelancer  
(NoVa/DC/Remote); see [http://www.Codosaur.us/](http://www.Codosaur.us/).
