Strange SDBM behaviour

hi gurus and nubys,

I just noticed this:

db=SDBM.new 'foo', 0666

=> #<SDBM:0x28b3130>

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 :slight_smile:

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 ==

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);

···

In message "strange SDBM behaviour" on 04/06/10, gabriele renzi <surrender_it@rc1.vip.ukl.yahoo.com> writes:

It seem that = uses to_s while uses to_str:

-
- valstr = rb_obj_as_string(valstr);
+ StringValue(valstr);
     val.dptr = RSTRING(valstr)->ptr;