This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Using Berkeley DB Databases
CodeExpectedActual
require 'dbm'
DBM.open('random_thoughts') do |db|
  db['tape measure'] = 
	"What if there was a tape measure you could use as a yo-yo?"
  db[23] = "Fnord."
end
DBM.open('random_thoughts') do |db|
  puts db['tape measure']
  puts db['23']
end
What if there was a tape measure you could use as a yo-yo?
Fnord.
What if there was a tape measure you could use as a yo-yo?
Fnord.
DBM.open('random_thoughts') { |db| db[23] }
TypeError: can't convert Fixnum into String
...
TypeError: can't convert Fixnum into String
	from (irb):11:in `[]'
	from (irb):11
	from (irb):11
Dir['random_thoughts.*']
["random_thoughts.pag", "random_thoughts.dir"] ["random_thoughts.pag", "random_thoughts.dir"]
require 'bdb'
db = BDB::Hash.create('random_thoughts2.db', nil, BDB::CREATE) 
db['Why do we park on a driveway but'] = 'it never rains but it pours.'
db.close
db = BDB::Hash.open('random_thoughts2.db', nil, 'r')
db['Why do we park on a driveway but']
"it never rains but it pours."
Error! (Exception?) Here's stdout:
NotImplementedError: 
BDB needs compatible versions of libdb & db.h
	you have db.h version 4.4.16 and libdb version 4.4.20

	from /usr/local/lib/site_ruby/1.8/i486-linux/bdb.so
	from (irb):13
NameError: uninitialized constant BDB
	from (irb):14
NoMethodError: undefined method `[]=' for nil:NilClass
	from (irb):15
NoMethodError: undefined method `close' for nil:NilClass
	from (irb):16
NameError: uninitialized constant BDB
	from (irb):17
NoMethodError: undefined method `[]' for nil:NilClass
	from (irb):18
db.close
db = BDB::Btree.create('element_reviews.db', nil, BDB::CREATE) 
db['earth'] = 'My personal favorite element.'
db['water'] = 'An oldie but a goodie.'
db['air'] = 'A good weekend element when you're bored with other elements.'
db['fire'] = 'Perhaps the most overrated element.'
db.each { |k,v| puts k }
air
earth
fire
water
Error! (Exception?) Here's stdout:
NoMethodError: undefined method `close' for nil:NilClass
	from (irb):19
NameError: uninitialized constant BDB
	from (irb):20
NoMethodError: undefined method `[]=' for nil:NilClass
	from (irb):21
NoMethodError: undefined method `[]=' for nil:NilClass
	from (irb):22
db['water']
"An oldie but a goodie."
Error! (Exception?) Here's stdout:
db.close
...