indigo0086 indigo0086 - 6 months ago 27
Ruby Question

using bundler to load different versions of gems for different platforms

So I am developing a Sinatra for both windows and linux. The problem is I'm using Thin instead of Webrick and eventmachine for windows only works with a pre-release version while linux uses the latest stable. in the gemfile you of course cannot include the same gem with different versions like so:

gem "eventmachine", "~> 1.0.0.beta.4.1", :group => :development_win
gem "eventmachine", group => :development_linux
gem "thin


I was wondering if there was a way to work around this, maybe using one gemfile for windows and one gemfile for linux, what would the command be to load one or the other.

Alternatively is there a way to perhaps in git manage just the gemfile for two different platforms, perhaps through a branch for just the file (don't know if that's possible from what I've read of git branches).

Answer

You can do it like that:

# Windows
gem "eventmachine", "~> 1.0.0.beta.4.1", :platform => [:mswin, :mingw]

# C Ruby (MRI) or Rubinius, but NOT Windows
gem "eventmachine", :platform => :ruby

Full list of available platforms:

ruby      C Ruby (MRI) or Rubinius, but NOT Windows
ruby_18   ruby AND version 1.8
ruby_19   ruby AND version 1.9
ruby_20   ruby AND version 2.0
mri       Same as ruby, but not Rubinius
mri_18    mri AND version 1.8
mri_19    mri AND version 1.9
mri_20    mri AND version 2.0
rbx       Same as ruby, but only Rubinius (not MRI)
jruby     JRuby
mswin     Windows
mingw     Windows 'mingw32' platform (aka RubyInstaller)
mingw_18  mingw AND version 1.8
mingw_19  mingw AND version 1.9
mingw_20  mingw AND version 2.0 

You can find more information in Gemfile(5) man page here (see 'Platforms' section).

Another approach is to use RUBY_PLATFORM constant:

if RUBY_PLATFORM =~ /win32/
  gem "eventmachine", "~> 1.0.0.beta.4.1"
else
  gem "eventmachine"
end

I haven't seen full list of available values for RUBY_PLATFORM but you can run

ruby -e 'puts RUBY_PLATFORM'

on both your platforms and see the difference.