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:

1
2
3
4
5
6
7
8
require 'sdbm'
 
SDBM.open 'my_database' do |db|
  db['apple'] = 'fruit'
  db['pear'] = 'fruit'
  db['carrot'] = 'vegetable'
  db['tomato'] = 'vegetable'
end

Bulk update:

1
2
3
4
5
require 'sdbm'
 
SDBM.open 'my_database' do |db|
  db.update('peach' => 'fruit', 'tomato' => 'fruit')
end

Retrieve values:

1
2
3
4
5
6
7
require 'sdbm'
 
SDBM.open 'my_database' do |db|
  db.each do |key, value|
    puts "Key: #{key}, Value: #{value}"
  end
end

Outputs:

1
2
3
4
5
Key: apple, Value: fruit
Key: pear, Value: fruit
Key: carrot, Value: vegetable
Key: peach, Value: fruit
Key: tomato, Value: fruit
delete_if
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.delete_if { |key, value| ... } â self Instance Public methods Iterates

2025-01-10 15:47:30
key?
  • References/Ruby on Rails/Ruby/Classes/SDBM

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

2025-01-10 15:47:30
has_value?
  • References/Ruby on Rails/Ruby/Classes/SDBM

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

2025-01-10 15:47:30
keys
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.keys â Array Instance Public methods Returns a new Array containing the

2025-01-10 15:47:30
[]=
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm[key] = value â value Instance Public methods Stores a new value

2025-01-10 15:47:30
update
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.update(pairs) â self Instance Public methods Insert or update key-value

2025-01-10 15:47:30
empty?
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.empty? â true or false Instance Public methods Returns true

2025-01-10 15:47:30
close
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.close â nil Instance Public methods Closes the database file.

2025-01-10 15:47:30
replace
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.replace(pairs) â self Instance Public methods Empties the database, then

2025-01-10 15:47:30
clear
  • References/Ruby on Rails/Ruby/Classes/SDBM

sdbm.clear â self Instance Public methods Deletes all data from the database

2025-01-10 15:47:30