strange SDBM behaviour

G

gabriele renzi

hi gurus and nubys,

I just noticed this:
db=SDBM.new 'foo', 0666
=> # said:
db[:foo]=10 => 10
db.keys => ["foo"]
db[:foo]
TypeError: cannot convert Symbol into String
from (irb):8:in `[]'
from (irb):8

It seem that []= uses to_s while [] uses to_str:
class C
def to_s
'miao'
end
def to_str
raise 'fuku fukissimo fuku'
end
end => nil
db[C.new]=30 => 30
db.keys => ["foo", "miao"]
db[C.new]
RuntimeError: fuku fukissimo fuku
from (irb):16:in `to_str'
from (irb):21:in `[]'
from (irb):21

is this an expected behaviour? I believe wheter SDBM uses the one or
the other it should be consistent in hi choice :)

Anyway:
ruby 1.8.1 (2003-12-25) [i386-mswin32]

Can't say what version sdbm is, given that VERSION refers to 1.8.1 and
SDBM::constants == []
 
Y

Yukihiro Matsumoto

In message "strange SDBM behaviour"

|It seem that []= uses to_s while [] uses to_str:

It's a bug in sdbm. Thank you for reporting.
Here the patch for ones who need.

matz.

--- ext/sdbm/init.c 8 May 2004 08:12:00 -0000 1.21
+++ ext/sdbm/init.c 10 Jun 2004 10:22:14 -0000
@@ -477,3 +477,3 @@ fsdbm_store(obj, keystr, valstr)
fdbm_modify(obj);
- keystr = rb_obj_as_string(keystr);
+ StringValue(keystr);

@@ -482,5 +482,3 @@ fsdbm_store(obj, keystr, valstr)

- if (NIL_P(valstr)) return fsdbm_delete(obj, keystr);
-
- valstr = rb_obj_as_string(valstr);
+ StringValue(valstr);
val.dptr = RSTRING(valstr)->ptr;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top