From: Clemens Hintze Date: 2000-03-28T14:09:10+02:00 Subject: [ruby-talk:02214] *DBM#invert --Multipart_Tue_Mar_28_14:09:10_2000-1 Content-Type: text/plain; charset=US-ASCII There seems really to be a bug! DBM#invert will build a hash during execution that contains all values as keys and the keys as values. That means your demo 'mdb.each { ... }' should return: 1--a 2--b But the bug seems to be that DBM#invert returns the DBM instance itself instead of the new-created hash! So in your example DBM#each is called instead of Hash#each. The same is true for GDBM! Applying following patches should help: --Multipart_Tue_Mar_28_14:09:10_2000-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="dbm.c.patch" Content-Transfer-Encoding: quoted-printable *** ext/dbm/dbm.c_sv Tue Mar 7 09:37:52 2000 --- ext/dbm/dbm.c Tue Mar 28 13:57:05 2000 *************** *** 262,268 **** valstr =3D rb_tainted_str_new(val.dptr, val.dsize); rb_hash_aset(hash, valstr, keystr); } ! return obj; } = static VALUE --- 262,268 ---- valstr =3D rb_tainted_str_new(val.dptr, val.dsize); rb_hash_aset(hash, valstr, keystr); } ! return hash; } = static VALUE --Multipart_Tue_Mar_28_14:09:10_2000-1 Content-Type: text/plain; charset=US-ASCII --Multipart_Tue_Mar_28_14:09:10_2000-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="gdbm.c.patch" Content-Transfer-Encoding: quoted-printable *** ext/gdbm/gdbm.c_sv Tue Mar 7 09:37:53 2000 --- ext/gdbm/gdbm.c Tue Mar 28 13:57:37 2000 *************** *** 260,266 **** valstr =3D rb_tainted_str_new(val.dptr, val.dsize); rb_hash_aset(hash, valstr, keystr); } ! return obj; } = static VALUE --- 260,266 ---- valstr =3D rb_tainted_str_new(val.dptr, val.dsize); rb_hash_aset(hash, valstr, keystr); } ! return hash; } = static VALUE --Multipart_Tue_Mar_28_14:09:10_2000-1 Content-Type: text/plain; charset=US-ASCII HTH, \cle ts writes: > > classes *DBM has a method #invert, but this method don't seem to work or > I've not understood how to use it : > > pigeon% cat b.rb > #!/usr/bin/ruby > require "gdbm" > dbm = GDBM.open("toto") > dbm["a"] = "1" > dbm["b"] = "2" > dbm.each { |x, y| print "#{x}--#{y}\n" } > mbd = dbm.invert > mbd.each { |x, y| print "#{x}--#{y}\n" } > dbm.close > pigeon% b.rb > a--1 > b--2 > a--1 > b--2 > pigeon% > > > Guy Decoux -- Clemens Hintze mailto: c.hintze@gmx.net --Multipart_Tue_Mar_28_14:09:10_2000-1--