[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

writing objects to files

Radu Spineanu

8/23/2006 10:43:00 PM

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

10 Answers

Justin Collins

8/23/2006 10:50:00 PM

0

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/Ma...
[2]http://ruby-doc.org/stdlib/libdoc/yaml/rdoc/...

Tim Hunter

8/23/2006 10:51:00 PM

0

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

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

Marc Heiler

8/24/2006 2:14:00 AM

0

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

James Gray

8/24/2006 2:20:00 AM

0

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.

Hope that helps.

James Edward Gray II

Hal E. Fulton

8/24/2006 2:22:00 AM

0

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

8/24/2006 6:52:00 AM

0

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.

Fred
--
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/24/2006 9:24:00 PM

0

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

8/25/2006 9:59:00 AM

0

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

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

Ironically, history says a different thing. :-)

> Fred
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

William Crawford

8/25/2006 10:31:00 AM

0

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

Christian Neukirchen

8/25/2006 1:25:00 PM

0

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