How do you save the contents of an object?

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.

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

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

Hope this helps!

On 10/23/06, Steve Q. [email protected] 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/.


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

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

OCS Ruby Forums - http://www.rubyforums.com/
My Blog - http://www.rwoliver.com/

On 10/23/06, Steve Q. [email protected] 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/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/.

HTH,
Michael G.

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”

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

On 24/10/06, Ilan B. [email protected] 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

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

Hope it helps!
Gustav P.
[email protected]

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

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

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