Type:
Class
Constants:
READER : INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT)

Indicates that dbm_open() should open the database in read-only mode

WRITER : INT2FIX(O_RDWR|RUBY_DBM_RW_BIT)

Indicates that dbm_open() should open the database in read/write mode

WRCREAT : INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT)

Indicates that dbm_open() should open the database in read/write mode, and create it if it does not already exist

NEWDB : INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT)

Indicates that dbm_open() should open the database in read/write mode, create it if it does not already exist, and delete all contents if it does already exist.

VERSION : version

Identifies ndbm library version.

Examples:

  • ândbm (4.3BSD)â

  • âBerkeley DB 4.8.30: (April 9, 2010)â

  • âBerkeley DB (unknown)â (4.4BSD, maybe)

  • âGDBM version 1.8.3. 10/15/2002 (built Jul 1 2011 12:32:45)â

  • âQDBM 1.8.78â

Documented by mathew meta@pobox.com.

Introduction

The DBM class provides a wrapper to a Unix-style dbm or Database Manager library.

Dbm databases do not have tables or columns; they are simple key-value data stores, like a Ruby Hash except not resident in RAM. Keys and values must be strings.

The exact library used depends on how Ruby was compiled. It could be any of the following:

  • The original ndbm library is released in 4.3BSD. It is based on dbm library in Unix Version 7 but has different API to support multiple databases in a process.

  • Berkeley DB versions 1 thru 5, also known as BDB and Sleepycat DB, now owned by Oracle Corporation.

  • Berkeley DB 1.x, still found in 4.4BSD derivatives (FreeBSD, OpenBSD, etc).

  • gdbm, the GNU implementation of dbm.

  • qdbm, another open source reimplementation of dbm.

All of these dbm implementations have their own Ruby interfaces available, which provide richer (but varying) APIs.

Cautions

Before you decide to use DBM, there are some issues you should consider:

  • Each implementation of dbm has its own file format. Generally, dbm libraries will not read each other's files. This makes dbm files a bad choice for data exchange.

  • Even running the same OS and the same dbm implementation, the database file format may depend on the CPU architecture. For example, files may not be portable between PowerPC and 386, or between 32 and 64 bit Linux.

  • Different versions of Berkeley DB use different file formats. A change to the OS may therefore break DBM access to existing files.

  • Data size limits vary between implementations. Original Berkeley DB was limited to 2GB of data. Dbm libraries also sometimes limit the total size of a key/value pair, and the total size of all the keys that hash to the same value. These limits can be as little as 512 bytes. That said, gdbm and recent versions of Berkeley DB do away with these limits.

Given the above cautions, DBM is not a good choice for long term storage of important data. It is probably best used as a fast and easy alternative to a Hash for processing large amounts of data.

Example

require 'dbm'
db = DBM.open('rfcs', 666, DBM::CREATRW)
db['822'] = 'Standard for the Format of ARPA Internet Text Messages'
db['1123'] = 'Requirements for Internet Hosts - Application and Support'
db['3068'] = 'An Anycast Prefix for 6to4 Relay Routers'
puts db['822']
include?

dbm.has_key?(key) â boolean Instance Public methods Returns true if the database

2015-04-02 20:06:52
[]

dbm[key] â string value or nil Instance Public methods Return a value from

2015-04-02 19:07:23
shift

dbm.shift() â [key, value] Instance Public methods Removes a [key, value] pair

2015-04-02 20:50:37
values_at

dbm.values_at(key, ...) â Array Instance Public methods Returns an array containing

2015-04-02 21:30:53
close

dbm.close Instance Public methods Closes the database.

2015-04-02 19:21:26
[]=

dbm[key] = value Instance Public methods Stores the specified string value in

2015-04-02 19:14:38
values

dbm.values â array Instance Public methods Returns an array of all the string

2015-04-02 21:26:26
member?

dbm.has_key?(key) â boolean Instance Public methods Returns true if the database

2015-04-02 20:35:48
has_value?

dbm.has_value?(value) â boolean Instance Public methods Returns true if the

2015-04-02 20:04:43
store

dbm.store(key, value) â value Instance Public methods Stores the specified string

2015-04-02 21:01:02