views:

423

answers:

2

Is it possible to conditionally set a dependency on a gem at install time? Here's my situation which will hopefully clarify the question.

There is a gem I maintain that depends on the json gem. However, I frequently use jruby which has a port of the json gem that's called json-jruby. To work around this I have to build two separate gems for each dependency. I'd like to build one gem that either says it depends on json OR json-jruby, or it checks at install time and uses the right dependency.

Anyone have a good solution to this?

+2  A: 

Hmm,

I believe one can code multiple dependencies because I've noticed gems which seemed to do this. But I would you advise against doing this. No matter how simple the code you are inserting here is, when you consider that the user will be loading one gem which depends on another which depends on another, even a small glitch in the logic of one piece of code or another can make the whole dependency train a nightmare.

So, I'd leave your kludge as-is. Further, I'd suggest you have a tarball that users can download as well. I've had to manually extract the code inside of gems whose configuration code wouldn't let me install them.

Joe Soul-bringer
+2  A: 

checkout a somewhat tutorial here: http://wiki.github.com/rdp/ruby%5Ftutorials%5Fcore/gem

it shows how to install different versions of dependencies depending on what version of ruby the installee is using.

(short answer--it ain't as easy as it should be)

rogerdpack
Link has moved to: http://en.wikibooks.org/wiki/Ruby_Programming/RubyGems#How_to_install_different_versions_of_gems_depending_on_which_version_of_ruby_the_installee_is_using
Peter Wagenet
Yehuda Katz says this is a bad idea. And suggests putting a conditional in the gemspec and building two versions.
Peter Wagenet
conditionals in the gemspec can control "jruby versus mri" but (as of yet) can't control say 1.8 versus 1.9 gems. I agree with yehuda it's a bad idea, but I'm unaware of a work around for it besides this currently.
rogerdpack