# How do you save the contents of an object?

**URL:** <https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354>\
**Category:** Ruby\
**Created:** [October 24, 2006, 2:17am UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354 "2006-10-24T02:17:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 2:17am UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/1 "2006-10-24T02:17:14Z")

</div>

Is there an easy way to save the contents of an object in ruby?

This is hard to google, so I am posting it up. I just need a general  
idea (if anyoen has it), you don’t have to spell it out for me.

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 2:25am UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/2 "2006-10-24T02:25:43Z")

</div>

Yes, it’s called Marshaling.

# Create a trivial object

a = “Object to save”

# Save it to stored\_object

stored\_object = Marshal.dump(a)

# Destroy “a”

a = nil

# Restore the object

a = Marshal.load(stored\_object)

You can read more about it here:  
[http://ruby-doc.org/core/classes/Marshal.html](http://ruby-doc.org/core/classes/Marshal.html)

Ruby makes it a breeze to implement. You can also use YAML to save your  
Marshaled object data via YAML’s dump and load methods. See more about  
that  
here: [http://ruby-doc.org/core/classes/YAML.html](http://ruby-doc.org/core/classes/YAML.html)

Hope this helps!

On 10/23/06, Steve Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there an easy way to save the contents of an object in ruby?
> 
> This is hard to google, so I am posting it up. I just need a general  
> idea (if anyoen has it), you don’t have to spell it out for me.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Robert W. Oliver II  
President, OCS Solutions, Inc. - Web Hosting and Development

> **[OCS Solutions Since 1997 Providing Reliable Quality Web Services](https://www.ocssolutions.com/)**
>
> OCS Solutions since 1997 provides quality and affordable managed web hosting, server admin, development and more. Contact us for the best reliable services!

Toll-Free Phone - 1-800-672-8415

OCS Ruby Forums - [http://www.rubyforums.com/](http://www.rubyforums.com/)  
My Blog - [http://www.rwoliver.com/](http://www.rwoliver.com/)

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 2:27am UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/3 "2006-10-24T02:27:44Z")

</div>

On 10/23/06, Steve Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there an easy way to save the contents of an object in ruby?

Check out these.

[http://www.ruby-doc.org/core/classes/Marshal.html](http://www.ruby-doc.org/core/classes/Marshal.html)  
[http://www.ruby-doc.org/core/classes/YAML.html](http://www.ruby-doc.org/core/classes/YAML.html)

This is hard to google, so I am posting it up. I just need a general

> idea (if anyoen has it), you don’t have to spell it out for me.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/4 "2006-10-24T16:47:05Z")

</div>

Steve Q. wrote:

> Is there an easy way to save the contents of an object in ruby?

As a further aside to saving with yaml, yet another fantastic little  
goody that RoR provides is the method .to\_yaml() …

ilan@iberci-pc:~/rails$ script/console  
Loading development environment.

> > “sample”.to\_yaml  
> > =\> “— sample\n”

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 2:27am UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/5 "2006-10-24T02:27:43Z")

</div>

Steve Q. wrote:

> Is there an easy way to save the contents of an object in ruby?
> 
> This is hard to google, so I am posting it up. I just need a general  
> idea (if anyoen has it), you don’t have to spell it out for me.

ri Marshal  
ri YAML

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/6 "2006-10-24T17:01:26Z")

</div>

On 24/10/06, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> “— sample\n”  
> You don’t need RoR for that. Merely including yaml will give objects  
> the to\_yaml method:

## irb(main):001:0\> require ‘yaml’ =\> true irb(main):002:0\> puts({“one”=\>1,“two”=\>2,“three”=\>3}.to\_yaml)

three: 3  
two: 2  
one: 1  
=\> nil

Farrel

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 24, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/7 "2006-10-24T17:27:35Z")

</div>

Steve Q. wrote:

> Is there an easy way to save the contents of an object in ruby?
> 
> This is hard to google, so I am posting it up. I just need a general  
> idea (if anyoen has it), you don’t have to spell it out for me.

I posted an article/tutorial on marshalling objects and saving them to  
files last night,  
you may want to throw a quick look at it as it sounds applicable to your  
question…

[http://rails.co.za/articles/2006/10/23/saving-ruby-objects-to-disc](http://rails.co.za/articles/2006/10/23/saving-ruby-objects-to-disc)

Hope it helps!  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 25, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/8 "2006-10-25T17:27:54Z")

</div>

The built-in Marshal class is designed for this very thing, and  
anecdotal evidence says it’s faster than YAML. YAML has the advantage  
of being readable to the naked eye, however. Here’s a good discussion  
on Marshal vs. YAML:

[http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/99119?99004-101444](http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/99119?99004-101444)

---

<div class="post-metadata">

**Author:** ![steveeq1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@steveeq1](https://www.ruby-forum.com/u/steveeq1)\
**Post date:** [October 25, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/how-do-you-save-the-contents-of-an-object/73354/9 "2006-10-25T12:31:30Z")

</div>

Use YAML you can store/load the content of a Ruby object without any  
difficulty.

Please visit YAML.rb at [http://yaml4r.sourceforge.net/](http://yaml4r.sourceforge.net/)  
The codebook is of great use to give you what is going on:  
[http://yaml4r.sourceforge.net/cookbook/](http://yaml4r.sourceforge.net/cookbook/)
