I need to use two gems in my project that both claim the PDF namespace: pdf-reader and htmldoc.
Is there any way to get them to play nice together? The only way I can think of is to rewrite my own version of htmldoc to give it a different namespace.
Basically, there's nothing you can do. It is good practice in Ruby, to use distinctive names in the top-level namespace precisely for this reason, and you just happened to stumble upon two libraries that violate that practice.
One thing you could do is to use Kernel#load
instead of Kernel#require
. Kernel#load
takes an optional boolean argument, which will tell it to evaluate the file within an anonymous module. Note, however, that this is in no way safe: it is perfectly possible to explicitly put stuff in the top-level namespace (using something like module ::PDF
) and thus break out of the anonymous module.
Note also that the API is really crappy: load
simply returns true
or false
, just like require
does. (Actually, since load
always loads, it always returns true
.) There is no way to actually get at the anonymous module. You basically have to grab it out of the ObjectSpace
by hand. Oh, and of course, since nothing actually references the anonymous module, it will be garbage-collected, so not only do you have to rummage around in the bowels of ObjectSpace
to find the module, you also have to race the garbage collector.
Sometimes, I wish Ruby had a proper module system like Newspeak, Standard ML or Racket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With