Forum: Ruby Shelve module in Ruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F7b9fcde68e1e8348da631dc38e0c29c?d=identicon&s=25 Bryan (Guest)
on 2005-12-20 18:28
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-20 18:37
(Received via mailing list)
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 Gray II
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2005-12-20 18:40
(Received via mailing list)
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.

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
280b41a88665fd8c699e83a9a25ef949?d=identicon&s=25 Stephen Waits (Guest)
on 2005-12-20 18:43
(Received via mailing list)
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][1] in addition to the other suggestions.

--Steve

[1]: http://madeleine.sourceforge.net/
F7b9fcde68e1e8348da631dc38e0c29c?d=identicon&s=25 Bryan (Guest)
on 2005-12-20 21:03
(Received via mailing list)
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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2005-12-21 23:20
(Received via mailing list)
James Edward Gray 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/doc/api/index.html
This topic is locked and can not be replied to.