Installing gems inside (J)Ruby code
I am using JRuby along with Cucumber and is looking for a way of running
jruby -S gem update --system
jruby -S gem install cucumber
from within the Java ScriptEngine. No amount of Googling have let me to a solution to this problem. Basically I want to be able to do something like this
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jRubyEngine = manager.getEngineByName("jruby");
: // 开发者_开发技巧some unknown code here
jRubeEngine.eval("call gems install/update from inside JRuby")
Is there a way of accomplishing this?
RubyGems is just a Ruby library. The gem
command is only a thin wrapper around the library. Everything you can do with the command, you can do with the library.
I've never actually used the library, but I guess what you want to look at is the Gem::DepencyInstaller and the code would look something like this (completely untested, just pulled out of my you-know-what):
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jRubyEngine = manager.getEngineByName("jruby");
String s = "
require 'rubygems'
require 'rubygems/dependency_installer'
Gem::DependencyInstaller.new.install('cucumber')
";
jRubyEngine.eval(s);
精彩评论