tags:

views:

146

answers:

5

Is it possible to add a gem dependency only if the person is using a certain version of ruby?

Background: I'm working on a fork of a project that uses Test::Unit::Autorunner and the like. They are part of the standard library in ruby 1.8, but aren't part of the standard library in 1.9.1, and is instead in the "test-unit" gem. I want to add a dependency that says that if someone's using ruby 1.9.1 or later, install the "test-unit" gem, but if they're using 1.8 or earlier, they don't need to install anything.

A: 

hay ... i'm kind of a ruby newbie ... if this is the best way to do it.

any way ... i wish i can do that using only Ruby .... though u can use your operating system shell to do that, by using this in your program installer, just execute (works for Linux based operating systems):

$ruby --version

(u can execute that from a ruby installer file, just like: ruby --version) and put a possibility according to output, if it's 1.9.1 add an extra line to execute:

$ sudo gem install gem_name

else, just leave it be.

Raafat
+2  A: 

If you look at the gemspec documentation for add_dependency, there isn't an option for a ruby version. Perhaps you could use the post_install_message attribute to tell the user to install the gem if they're using ruby 1.9.

A: 

You can't. You need to build two gems, one with

spec.required_ruby_version = '~> 1.8.6'

and one with

spec.required_ruby_version = '~> 1.9.1'
spec.add_dependency 'test-unit', '~> 2.0.5'
Jörg W Mittag
-1 Please don't do this. This screws up all kinds of dependency tools.
Bob Aman
@Bob Aman: do you happen to know what should be done?
Andrew Grimm
A: 

Gemspecs are just ruby files anyway, so you can execute any ruby code inside them, so:

spec.add_dependency = 'test-unit', '>= 2.0' if RUBY_VERSION =~ '1.9'

EDIT: Specs run only on the builders machine.

Mereghost
Where does the ruby code get executed? On the gem creator's machine, or on the installer's machine?
Andrew Grimm
Forget it. It will run only on the creator's machine. In order to don't generate a huge maintenace problem for you, you could present a post install message or use the pre_install or post_intall hooks to get the gem installed.I edited the answer.
Mereghost
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