# Writing objects to files

**URL:** <https://rubytalk.org/t/writing-objects-to-files/30274>\
**Category:** ruby-talk\
**Created:** [23 August 2006 22:43 UTC](https://rubytalk.org/t/writing-objects-to-files/30274 "2006-08-23T22:43:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radu\_Spineanu](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Radu\_Spineanu](https://rubytalk.org/u/Radu_Spineanu)\
**Post date:** [23 August 2006 22:43 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/1 "2006-08-23T22:43:03Z")

</div>

Hi

Is it possible to write a ruby object (like a hash for example) to a  
file, and then read it and use it accordingly ?

Thanks,  
Radu Spineanu

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [23 August 2006 22:50 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/2 "2006-08-23T22:50:06Z")

</div>

Radu Spineanu wrote:

> Hi
> 
> Is it possible to write a ruby object (like a hash for example) to a  
> file, and then read it and use it accordingly ?
> 
> Thanks,  
> Radu Spineanu  
> &nbsp;&nbsp;  
> Yes, please see Marshal[1] and YAML[2].

-Justin

[1][module Marshal - RDoc Documentation](http://ruby-doc.org/core/classes/Marshal.html)  
[2][http://ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [23 August 2006 22:51 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/3 "2006-08-23T22:51:01Z")

</div>

Radu Spineanu wrote:

> Hi
> 
> Is it possible to write a ruby object (like a hash for example) to a  
> file, and then read it and use it accordingly ?
> 
> Thanks,  
> Radu Spineanu

You can either use Marshal or yaml.

Look up Marshal at [RDoc Documentation](http://www.ruby-doc.org/core/)

Look up yaml at [RDoc Documentation](http://www.ruby-doc.org/stdlib/)

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [24 August 2006 02:14 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/4 "2006-08-24T02:14:00Z")

</div>

Let me followup:

When to use Marshal, when to use yaml?  
Does one of the two have a big advantage over the other?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [24 August 2006 02:20 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/5 "2006-08-24T02:20:08Z")

</div>

Marshal is C pretty fast, but it writes binary files that are not human readable. YAML is plain text, but not as zippy.

Hope that helps.

James Edward Gray II

> **···**
>
> On Aug 23, 2006, at 9:14 PM, Marc Heiler wrote:
> 
> > Let me followup:
> > 
> > When to use Marshal, when to use yaml?  
> > Does one of the two have a big advantage over the other?

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [24 August 2006 02:21 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/6 "2006-08-24T02:21:49Z")

</div>

Marc Heiler wrote:

> Let me followup:
> 
> When to use Marshal, when to use yaml?  
> Does one of the two have a big advantage over the other?

YAML is human readable/editable.

Marshal is more compact.

That's about it.

Hal

---

<div class="post-metadata">

**Author:** ![F\_Senault](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@F\_Senault](https://rubytalk.org/u/F_Senault)\
**Post date:** [24 August 2006 06:55 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/7 "2006-08-24T06:55:03Z")

</div>

And Marshal is not guaranteed to produce files compatible between  
different versions of Ruby, while YAML is.

Fred

> **···**
>
> Le 24 août 2006 à 04:20, James Edward Gray II a écrit :
> 
> > On Aug 23, 2006, at 9:14 PM, Marc Heiler wrote:
> > 
> > > Let me followup:
> > > 
> > > When to use Marshal, when to use yaml?  
> > > Does one of the two have a big advantage over the other?
> > 
> > Marshal is C pretty fast, but it writes binary files that are not  
> > human readable. YAML is plain text, but not as zippy.
> 
> --  
> Hold me near you So close I sear you  
> Seeing, believing Dreaming, decieving  
> A place between sleep and awake End of innocence, unending masquerade  
> That's where I'll wait for you (Nightwish, Sleepwalker)

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [24 August 2006 21:24 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/8 "2006-08-24T21:24:11Z")

</div>

F. Senault wrote:

> **···**
>
> > Le 24 août 2006 à 04:20, James Edward Gray II a écrit :
> > 
> > > On Aug 23, 2006, at 9:14 PM, Marc Heiler wrote:
> > > 
> > > > Let me followup:
> > > > 
> > > > When to use Marshal, when to use yaml?  
> > > > Does one of the two have a big advantage over the other?
> > > 
> > > Marshal is C pretty fast, but it writes binary files that are not human readable. YAML is plain text, but not as zippy.
> > 
> > And Marshal is not guaranteed to produce files compatible between  
> > different versions of Ruby, while YAML is.
> 
> -And- the YAML format is actually an open specification, so you get the added value of being able to read the same file in other programming languages - I have mild doubts there are parsers for whatever Marshal outputs for anything but Ruby.
> 
> David Vallner

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [25 August 2006 09:58 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/9 "2006-08-25T09:58:58Z")

</div>

"F. Senault" \<fred@lacave.net\> writes:

> > > Let me followup:
> > > 
> > > When to use Marshal, when to use yaml?  
> > > Does one of the two have a big advantage over the other?
> > 
> > Marshal is C pretty fast, but it writes binary files that are not  
> > human readable. YAML is plain text, but not as zippy.
> 
> And Marshal is not guaranteed to produce files compatible between  
> different versions of Ruby, while YAML is.

Ironically, history says a different thing. 🙂

> **···**
>
> > Le 24 août 2006 à 04:20, James Edward Gray II a écrit :
> > 
> > > On Aug 23, 2006, at 9:14 PM, Marc Heiler wrote:
> 
> > Fred
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![William\_Crawford](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@William\_Crawford](https://rubytalk.org/u/William_Crawford)\
**Post date:** [25 August 2006 10:30 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/10 "2006-08-25T10:30:44Z")

</div>

Christian Neukirchen wrote:

> "F. Senault" \<fred@lacave.net\> writes:
> 
> > > human readable. YAML is plain text, but not as zippy.
> > 
> > And Marshal is not guaranteed to produce files compatible between  
> > different versions of Ruby, while YAML is.
> 
> Ironically, history says a different thing. 🙂

Just because something has always been compatible doesn't mean it always  
will be. Since YAML is an open spec, and Ruby follows that spec, it is  
guaranteed. Nobody makes any guarantees about Marshal. (It isn't  
likely to change without good reason, I'm sure, but that's not a  
guarantee.)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [25 August 2006 13:25 UTC](https://rubytalk.org/t/writing-objects-to-files/30274/11 "2006-08-25T13:25:02Z")

</div>

William Crawford \<wccrawford@gmail.com\> writes:

> Christian Neukirchen wrote:
> 
> > "F. Senault" \<fred@lacave.net\> writes:
> > 
> > > > human readable. YAML is plain text, but not as zippy.
> > > 
> > > And Marshal is not guaranteed to produce files compatible between  
> > > different versions of Ruby, while YAML is.
> > 
> > Ironically, history says a different thing. 🙂
> 
> Just because something has always been compatible doesn't mean it always  
> will be. Since YAML is an open spec, and Ruby follows that spec, it is  
> guaranteed. Nobody makes any guarantees about Marshal. (It isn't  
> likely to change without good reason, I'm sure, but that's not a  
> guarantee.)

I was refering to a Ruby version that generated YAML not readable in  
other versions due to a missing header or something.

I don't know when Marshal changed the last time... I can't find a  
mention of changing MAJOR in the CVS history, and MINOR changes (I see  
only one) can be read with later versions.

I'd use some kind of plain-text storage (including YAML) for long-term  
storage of important data. Marshal would be fine for storing HTTP  
sessions, e.g. (And RI too... :^))

> **···**
>
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)
