Setting gems inside (J) Ruby code

I use JRuby with Cucumber and am looking for a way to work

jruby -S gem update --system jruby -S gem install cucumber 

from Java ScriptEngine . No amount of googling allowed me to solve 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 any way to accomplish this?

+6
java ruby jruby
source share
1 answer

RubyGems is just a Ruby library. The gem command is just a thin shell around the library. All that you can do with the command, you can do with the library.

I never used the library, but I think that you want to look, this is Gem :: DepencyInstaller , and the code will look something like this (completely untested, just pulled out of 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); 
+7
source share

All Articles