[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to support multiple DBM backends?

Tassilo Horn

5/19/2006 6:30:00 AM

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...
--
A child of five could understand this! Fetch me a child of five!
4 Answers

ts

5/19/2006 8:18:00 AM

0

>>>>> "T" == Tassilo Horn <heimdall@uni-koblenz.de> writes:

Just write it

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

db_backend = QDBM

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

--

Guy Decoux

Tassilo Horn

5/19/2006 9:58:00 AM

0

ts <decoux@moulon.inra.fr> writes:

>>>>>> "T" == Tassilo Horn <heimdall@uni-koblenz.de> writes:
>
> 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
--
My opinions may have changed, but not the fact that I am right.

Jeff Schwab

5/19/2006 3:04:00 PM

0

Tassilo Horn wrote:
> ts <decoux@moulon.inra.fr> writes:
>
>>>>>>> "T" == Tassilo Horn <heimdall@uni-koblenz.de> writes:
>> 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. :-(

Having two calls to methods with the same name, but different recipients
and parameter lists, is probably forgivable. :) Of course, you
shouldn't have to duplicate the body of the open-blocks.

,----
| def foo(dbm)
| ...
| end
|
| begin
| require 'qdbm'
| Depot.open(...) { |dbm| foo(dbm) }
| rescue LoadError
| require 'gdbm'
| GDBM.open(...) { |dbm| foo(dbm) }
| end
`----

Tassilo Horn

5/19/2006 6:31:00 PM

0

Jeffrey Schwab <jeff@schwabcenter.com> writes:

Hi Jeffrey,

> Having two calls to methods with the same name, but different
> recipients and parameter lists, is probably forgivable. :) Of course,
> you shouldn't have to duplicate the body of the open-blocks.

That's the way I'm going right now. :)

Bye,
Tassilo
--
A child of five could understand this! Fetch me a child of five!