Type:
Class

SDBM provides a simple file-based key-value store, which can only store String keys and values.

Note that Ruby comes with the source code for SDBM, while the DBM and GDBM standard libraries rely on external libraries and headers.

Examples

Insert values:

require 'sdbm'

SDBM.open 'my_database' do |db|
  db['apple'] = 'fruit'
  db['pear'] = 'fruit'
  db['carrot'] = 'vegetable'
  db['tomato'] = 'vegetable'
end

Bulk update:

require 'sdbm'

SDBM.open 'my_database' do |db|
  db.update('peach' => 'fruit', 'tomato' => 'fruit')
end

Retrieve values:

require 'sdbm'

SDBM.open 'my_database' do |db|
  db.each do |key, value|
    puts "Key: #{key}, Value: #{value}"
  end
end

Outputs:

Key: apple, Value: fruit
Key: pear, Value: fruit
Key: carrot, Value: vegetable
Key: peach, Value: fruit
Key: tomato, Value: fruit
each

sdbm.eachsdbm.each { |key, value| ... }sdbm.each_pairsdbm.each_pair { |key, value| ... } Instance

2015-05-14 03:27:25
delete

sdbm.delete(key) â value or nilsdbm.delete(key) { |key, value| ... } Instance Public methods

2015-05-14 03:21:23
[]

sdbm[key] â value or nil Instance Public methods Returns the value

2015-05-14 02:56:42
fetch

sdbm.fetch(key) â value or nilsdbm.fetch(key) { |key| ... } Instance Public methods

2015-05-14 03:43:52
has_key?

sdbm.has_key?(key) â true or false Instance Public methods Returns true

2015-05-14 03:48:21
values

sdbm.values â Array Instance Public methods Returns a new Array containing

2015-05-14 05:16:31
each_pair

sdbm.each_pairsdbm.each_pair { |key, value| ... } Instance Public methods Iterates

2015-05-14 03:32:21