Shelve module in Ruby?

Hi,

I'm fairly new to Ruby and I was wondering if there was a module that
functions similarly to the shelve module in Python.

The shelve module is a method for packaging a dictionary (hash) of
arbritrary objects into a file. I use it as a rudimentary database so
that I don't have to rebuild objects from text files over and over.

If anyone knows of a module in Ruby that could accomplish this, or a
suggestion on how to do this, any help would be appreciated.

Thanks,

Bryan Weatherly

Ruby comes with Marshal and YAML, which sounds like what you are looking for. Also see PStore, for transactional file storage.

Hope that helps.

James Edward Gray II

···

On Dec 20, 2005, at 11:27 AM, Bryan wrote:

Hi,

I'm fairly new to Ruby and I was wondering if there was a module that
functions similarly to the shelve module in Python.

The shelve module is a method for packaging a dictionary (hash) of
arbritrary objects into a file. I use it as a rudimentary database so
that I don't have to rebuild objects from text files over and over.

If anyone knows of a module in Ruby that could accomplish this, or a
suggestion on how to do this, any help would be appreciated.

Something like yaml? If I understand your request this is pretty close:

require 'yaml'

# arbitrary value (in this case a hash)
hash = { :foo => :bar, 'answer' => 42 }

# save it to a file
File.open( 'test.yaml', 'w') { |out| out.puts hash.to_yaml }

# read it back from a file
new_hash = YAML::load( File.open( 'test.yaml' ))

# show that they are equal
p hash
p new_hash

If you are curious the file is a readable text file:

···

On 12/20/05, Bryan <bryanweatherly@gmail.com> wrote:

The shelve module is a method for packaging a dictionary (hash) of
arbritrary objects into a file. I use it as a rudimentary database so
that I don't have to rebuild objects from text files over and over.

---
answer: 42
:foo: :bar

HIHI
pth

Also check out [Madeleine][1] in addition to the other suggestions.

--Steve

[1]: http://madeleine.sourceforge.net/

···

On Dec 20, 2005, at 9:27 AM, Bryan wrote:

I'm fairly new to Ruby and I was wondering if there was a module that
functions similarly to the shelve module in Python.

Thank you all for your speedy responses. YAML appears to be precisely
what I had in mind. I'm also going to take a look at Madeleine.

James Edward Gray II wrote:

···

On Dec 20, 2005, at 11:27 AM, Bryan wrote:

Hi,

I'm fairly new to Ruby and I was wondering if there was a module that
functions similarly to the shelve module in Python.

The shelve module is a method for packaging a dictionary (hash) of
arbritrary objects into a file. I use it as a rudimentary database so
that I don't have to rebuild objects from text files over and over.

If anyone knows of a module in Ruby that could accomplish this, or a
suggestion on how to do this, any help would be appreciated.

Ruby comes with Marshal and YAML, which sounds like what you are looking for. Also see PStore, for transactional file storage.

If you want something like PStore, but with both thread- and process-safe transactions, with finer granularity (treating a file tree as a single DB), and with configurable mapping to serialization types (including YAML and Marshal):

http://redshift.sourceforge.net/fsdb/
http://redshift.sourceforge.net/fsdb/doc/api/index.html

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407