Ruby gem equivalent of PERL storable

231 views Asked by At

Does Ruby have a gem equivalent of PERL's Storable?
I have tried rcstorable, but it only reads, it does not save.
Thanks.

2

There are 2 answers

0
svoop On

Take a look at PStore, maybe that's what you are looking for.

http://ruby-doc.org/stdlib-2.1.0/libdoc/pstore/rdoc/PStore.html

It's in the Stdlib, so no gem is required.

0
Neil Slater On

You can do the equivalent of Storable's freeze and thaw using Marshal:

In Perl:

use Storable;

my $serialised_data = freeze( $data_ref );

# and later

my $data_ref = thaw( $serialised_data );

In Ruby:

serialised_data = Marshal.dump( object );

# and later

object = Marshal.load( serialised_data );

One big difference - Storable covers more Perl library objects "out of the box" than Ruby's Marshal does, for non-core objects in Ruby sometimes you may need to add support for Marshal yourself. All the basic types - numbers, strings, arrays, hashes - work just fine though.