How to support multiple DBM backends?

T

Tassilo Horn

Hi,

I wrote a nice translation app (RDictCc, [1]), which uses GDBM. So
currently it's like

,----
| require 'gdbm'
|
| GDBM.open(...) do
| foo
| end
`----

Now I want to let it use QDBM if it's available, else fall back on
GDBM. But what's the most idiomatic way to do this?

Currently I'd go like this:

,----
| begin
| require 'qdbm'
| db_backend = :qdbm
| rescue LoadError
| require 'gdbm'
| db_backend = :gdbm
| end
|
| if db_backend == :qdbm
| QDBM.open(...) do |dbm|
| ...
| end
| else
| GDBM.open(...) do |dbm|
| ...
| end
| end
`----

But the if-statement seems smelly, since both GDBM and QDBM respond to
class method open(). But something like

,----
| require 'qdbm'
| db_backend = QDBM.class
| ...
| db_backend.open
`----

doesn't work.

Any hints?

Footnotes:
[1] http://www.uni-koblenz.de/~heimdall/apps.html
 
T

ts

Just write it

T> ,----
T> | require 'qdbm'
T> | db_backend = QDBM.class

db_backend = QDBM

T> | ...
T> | db_backend.open
T> `----
 
T

Tassilo Horn

ts said:
Just write it

T> ,----
T> | require 'qdbm'
T> | db_backend = QDBM.class

db_backend = QDBM

Ah, great. Unfortunately GDBM.open() and Depot.open() (that's the QDBM
thing) have different parameter list langths, so I cannot work arround
the if-statement anyway. :-(

Bye,
Tassilo
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top