# Shelve module in Ruby?

**URL:** <https://www.ruby-forum.com/t/shelve-module-in-ruby/50741>\
**Category:** Ruby\
**Created:** [December 20, 2005, 6:28pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741 "2005-12-20T18:28:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 20, 2005, 6:28pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/1 "2005-12-20T18:28:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 20, 2005, 6:37pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/2 "2005-12-20T18:37:33Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 20, 2005, 6:40pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/3 "2005-12-20T18:40:35Z")

</div>

On 12/20/05, Bryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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:

answer: 42  
:foo: :bar

HIHI  
pth

---

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 20, 2005, 6:43pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/4 "2005-12-20T18:43:38Z")

</div>

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.

Also check out [Madeleine](http://madeleine.sourceforge.net/) in addition to the other suggestions.

–Steve

---

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 20, 2005, 9:03pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/5 "2005-12-20T21:03:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/db5fbb/32.png) [@bryan](https://www.ruby-forum.com/u/bryan)\
**Post date:** [December 21, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/shelve-module-in-ruby/50741/6 "2005-12-21T23:20:55Z")

</div>

James Edward G. II wrote:

> > 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/)  
[http://redshift.sourceforge.net/fsdb/doc/api/index.html](http://redshift.sourceforge.net/fsdb/doc/api/index.html)
