Ruby 上的 Berkeley DB 中的键和值必须是字符串,而不是 int、float 或任何其他类型吗?
似乎如果我在 Ruby 上使用 Berkeley DB (DBM),哈希的键和值必须是字符串?它们可以是其他数据类型吗?
require 'dbm'
d = DBM.open('test1')
d[1] = 2
d[123] = 456
d[2] = 2.34
d['wa'] = Time.now.to_f
p d.keys
p d.values
结果:
C:\>ruby test_dbm.rb
["wa", "2", "1", "123"]
["1259229787.686", "2.34", "2", "456"]
It seems that if I use Berkeley DB (DBM) on Ruby, the hash's keys and values must be strings? Can they be other data type?
require 'dbm'
d = DBM.open('test1')
d[1] = 2
d[123] = 456
d[2] = 2.34
d['wa'] = Time.now.to_f
p d.keys
p d.values
Result:
C:\>ruby test_dbm.rb
["wa", "2", "1", "123"]
["1259229787.686", "2.34", "2", "456"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它们确实将键值存储为任意字节字符串,可以是固定长度或可变长度。我最近还意识到,当我必须在我们的一个项目中使用 bdb 时。
在命令行上查看键值
您可以通过键入字符串 full/bdb/file/path
Yes, they do store key values as arbitrary byte strings that can be fixed-length or variable-length. I also realised that recently when I had to use bdb in one of our projects.
And you can see the key values on command line by typing
strings full/bdb/file/path