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
new

SDBM.new(filename, mode = 0666) Class Public methods Creates a new database

2015-05-14 02:45:37
fetch

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

2015-05-14 03:43:52
values

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

2015-05-14 05:16:31
has_key?

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

2015-05-14 03:48:21
delete

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

2015-05-14 03:21:23
each_pair

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

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

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

2015-05-14 02:56:42