Writing objects to files

(Radu Spineanu) #1

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

(Collins, Justin) #2

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]http://ruby-doc.org/core/classes/Marshal.html
[2]http://ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html

(Tim Hunter) #3

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 http://www.ruby-doc.org/core/

Look up yaml at http://www.ruby-doc.org/stdlib/

(Marc Heiler) #4

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

(James Edward Gray II) #5

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?

(HAL 9000) #6

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

(F. Senault) #7

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)

(David Vallner) #8

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

(Christian Neukirchen) #9

"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

(William Crawford) #10

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

(Christian Neukirchen) #11

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