Different behaviour of -r depending on what you run it from

Hi all.

Back-story might be important here, so I will go into it a bit. Our
application embeds JRuby (technically we embed javax.script, not just
JRuby) and thus we have our own launcher for launching scripts. Now
I’m trying to fire up rspec from inside this script and the most
convenient way to do that seemed to be to deploy the gems in a jar
file, which is a pretty well-known strategy by now.

So I tried to do this:

require File.join(File.dirname(__FILE__), 

require ‘rspec’

This fails because gem can’t find rspec.

But that’s odd, I see people blogging about being able to do this:

So it should work, right? I thought that perhaps it was a different in
JRuby versions, so I grabbed the same version that person was using
and tried that too, and I found that no, the command they wrote didn’t
work for the same version they said it worked for:

irb-jruby-1.6.3 -r lib/jruby-testing-gems.jar
irb-jruby-1.7.3 -r lib/jruby-testing-gems.jar

I can’t remember why I tried this next, but I tried jruby -S irb

$ jruby-1.7.3 -r lib/jruby-testing-gems.jar -S irb
gems.jar -S irb
irb(main):001:0> require 'rspec'
=> true

irb --help seems to say that -r is the same as ruby -r, but
experimental evidence would appear to contradict this claim.

I am guessing that the reality is that there is a difference:

jruby-1.7.3 -r something -S irb        # Requires the jar before IRB
irb-jruby-1.7.3 -r something         # Requires IRB before the jar

And that in the latter case, because rubygems is probably loaded first
as a side-effect, the -r on the second command is too late to add any
gems. Maybe this isn’t mentioned in the docs because IRB isn’t part of
JRuby and the only place where this affects the behaviour is when
requiring a jar, which is of course JRuby-specific.

And following on from all this, I assume that the require statement I
originally wrote to try and require additional gems from a script is
way too late to have any effect at all.