1

On Ruby, when using DBM

require "dbm"

db = DBM.open("somedata")
db[1] = 2   # ok
p db[1]     # gives error

does anyone know db[1] = 2 is ok, but printing out db[1] will give error?

If it requires db["1"] to be valid, then how come it doesn't apply to both cases but to one case only?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
nonopolarity
  • 146,324
  • 131
  • 460
  • 740

1 Answers1

2

dbm convert key and value to string, so :

p db["1"]

give

"2"

raubarede
  • 36
  • 1
  • yes, dbm stores keys only as strings. so saying db[1] = 2 is really saying db["1"] = 2. when you access the data, you need to use the string. – Jaime Bellmyer Apr 16 '11 at 16:31