My case is that I have a Rails app. It uses Bundler to manage its Gems. I also need to run some processing using an old version of one of my own libraries which also has dependencies and gems that need resolving. I chose to make this a small command line app with an executable ruby file. This little tool uses Bundler to manage its Gems.
I will shell out to this executable from my Rails app. Simple, right? I thought it would, but there was a gotcha. The Rails app executes the external ruby file within the same "bundle" as the Rails app. I.E. I got the current versions of my lib and all gems.
After much Googling I combined one Stack Overflow answer with a note on some blog (lost both references).
some_result_value = "" Bundler.with_clean_env do Dir.chdir "/path/to/rubytool" some_result_value = `./bin/rubytool param1 param2` end
The key details here are the clean environment AND that I change directory before executing. I don't understand why changing the folder would be significant. It may even have been bad late-night mojo.
Anyway, as long as the command line tool being called uses bundler correctly it works ar intended. It is run with its own bundle of gems.
I would like to find a way to control this from the receiving ruby script but I have not looked into that yet.