Writing objects to files

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

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
  
Yes, please see Marshal[1] and YAML[2].

-Justin

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

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

Look up yaml at RDoc Documentation

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/.

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?

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

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)

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

"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. :slight_smile:

···

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

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. :slight_smile:

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/\.

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. :slight_smile:

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