Optional Rails initializer loading depending on Ruby plaform

Hey,

I found myself having to use jRuby in my app for a very very small but essential piece of code. The code in question is bundled in a gem which I require in Bundler under the jRuby platform, with all the rest of my gems put under MRI. This was unfortunately necessary otherwise if I don’t put the rest under MRI Bundler will try to download jRuby versions of every gem, which eventually breaks if a gem uses any C extensions.

This has worked for me fine, but it required that I patch all my initializers to make them load conditionally, i.e. depending on which platform they are part of in Bundler. Because Rails treats them as regular files with no mapping to their gems (Most of the files are gem initializers in my case, but I concede that it might not the case for everyone), they will load, and since some are not installed for one platform or the other, this errors out.

Has anyone been subject to problems like this? There are many solutions for my case, but I would like to know what people think about this or if someone has solved this before in other way.

I think you’re doing it on the right way, patching the initialiser.

At Gemfile you have to use:

platform :ruby do

gem ‘mri-specific-gem’

end

plataform :jruby do

gem ‘jruby-specific-gem’

end

And if you need to something in a initialiser, you may check the platform using bundler stuff:

if Bundler.current_ruby.jruby?

jruby patch code

end

if Bundler.current_ruby.ruby?

ruby patch code

end

It’s working great here :wink:

PS: I’m not sure if it’s ruby or mri, make sure before using it.

Yep, that’s exactly what I am doing. I was just wondering if people are solving it some either way, or even more ambitiously if someone is using Rails itself to solve this issue.