Shelve module in Ruby?

B

Bryan

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
 
J

James Edward Gray II

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.

Hope that helps.

James Edward Gray II
 
P

Patrick Hurley

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.

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

require 'yaml'

# arbitrary value (in this case a hash)
hash =3D { :foo =3D> :bar, 'answer' =3D> 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 =3D 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:
---
answer: 42
:foo: :bar

HIHI
pth
 
B

Bryan

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

Joel VanderWerf

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top